import-sync icon indicating copy to clipboard operation
import-sync copied to clipboard

`instanceof` appears to be broken

Open robross0606 opened this issue 9 months ago • 9 comments

Tried using this with @kubernetes/client-node and it failed at syncInclude() call:

const importSync = require('import-sync')
const k8s = importSync('@kubernetes/client-node')

Yielded:

    Failed to import from:
      @kubernetes/client-node
    Options:
      {}
    Require error message:
      YAMLException: Specified list of YAML types (or a single Type object) contains a non-Type object.
      at node_modules/js-yaml/lib/schema.js:104:13
          at Array.forEach (<anonymous>)
      at Schema.extend (node_modules/js-yaml/lib/schema.js:102:12)
      at new Schema (node_modules/js-yaml/lib/schema.js:62:15)
      at Object.<anonymous> (node_modules/js-yaml/lib/schema/failsafe.js:11:18)
      at Object.<anonymous> (node_modules/@httptoolkit/esm/esm.js:1:279304)
      at Object.<anonymous> (node_modules/@httptoolkit/esm/esm.js:1:315535)
      at Object.<anonymous>.require.Module._extensions (node_modules/@httptoolkit/esm/esm.js:1:163)
      at Object.Cp (node_modules/@httptoolkit/esm/esm.js:1:314277)
      at p (node_modules/@httptoolkit/esm/esm.js:1:315579)
      at cl (node_modules/@httptoolkit/esm/esm.js:1:275662)
      at Object.<anonymous> (node_modules/@httptoolkit/esm/esm.js:1:316224)
      at Object.<anonymous> (node_modules/@httptoolkit/esm/esm.js:1:257086)
      at Object.<anonymous>.require.Module._extensions (node_modules/@httptoolkit/esm/esm.js:1:163)
      at Object.Cp (node_modules/@httptoolkit/esm/esm.js:1:314277)
      at Object.u (node_modules/@httptoolkit/esm/esm.js:1:316406)
      at Object.o (node_modules/@httptoolkit/esm/esm.js:1:315227)
      at Object.<anonymous> (node_modules/@httptoolkit/esm/esm.js:1:312984)
      at Object.apply (node_modules/@httptoolkit/esm/esm.js:1:228557)
      at node_modules/@httptoolkit/esm/esm.js:1:279876
      at Object.<anonymous> (node_modules/@httptoolkit/esm/esm.js:1:279918)
      at node_modules/@httptoolkit/esm/esm.js:1:282340
      at node_modules/@httptoolkit/esm/esm.js:1:282416
      at vu (node_modules/@httptoolkit/esm/esm.js:1:257110)
      at Proxy.<anonymous> (node_modules/@httptoolkit/esm/esm.js:1:282190)
      at Object.<anonymous> (node_modules/@httptoolkit/esm/esm.js:1:280271)
      at Object.t (node_modules/@httptoolkit/esm/esm.js:1:307870)
      at n (node_modules/@httptoolkit/esm/esm.js:1:307684)
      at Object.<anonymous> (node_modules/js-yaml/lib/schema/json.js:12:18)
      at Object.<anonymous> (node_modules/@httptoolkit/esm/esm.js:1:279304)
      at Object.<anonymous> (node_modules/@httptoolkit/esm/esm.js:1:315535)
      at Object.<anonymous>.require.Module._extensions (node_modules/@httptoolkit/esm/esm.js:1:163)
      at Object.Cp (node_modules/@httptoolkit/esm/esm.js:1:314277)
      at p (node_modules/@httptoolkit/esm/esm.js:1:315579)
      at cl (node_modules/@httptoolkit/esm/esm.js:1:275662)
      at Object.<anonymous> (node_modules/@httptoolkit/esm/esm.js:1:316224)
      at Object.<anonymous> (node_modules/@httptoolkit/esm/esm.js:1:257086)
      at Object.<anonymous>.require.Module._extensions (node_modules/@httptoolkit/esm/esm.js:1:163)
      at Object.Cp (node_modules/@httptoolkit/esm/esm.js:1:314277)
      at Object.u (node_modules/@httptoolkit/esm/esm.js:1:316406)
      at Object.o (node_modules/@httptoolkit/esm/esm.js:1:315227)
      at Object.<anonymous> (node_modules/@httptoolkit/esm/esm.js:1:312984)
      at Object.apply (node_modules/@httptoolkit/esm/esm.js:1:228557)
      at node_modules/@httptoolkit/esm/esm.js:1:279876
      at Object.<anonymous> (node_modules/@httptoolkit/esm/esm.js:1:279918)
      at node_modules/@httptoolkit/esm/esm.js:1:282340
      at node_modules/@httptoolkit/esm/esm.js:1:282416
      at vu (node_modules/@httptoolkit/esm/esm.js:1:257110)
      at Proxy.<anonymous> (node_modules/@httptoolkit/esm/esm.js:1:282190)
      at Object.<anonymous> (node_modules/@httptoolkit/esm/esm.js:1:280271)
      at Object.t (node_modules/@httptoolkit/esm/esm.js:1:307870)
      at n (node_modules/@httptoolkit/esm/esm.js:1:307684)
      at Object.<anonymous> (node_modules/js-yaml/lib/schema/core.js:11:18)
      at Object.<anonymous> (node_modules/@httptoolkit/esm/esm.js:1:279304)
      at Object.<anonymous> (node_modules/@httptoolkit/esm/esm.js:1:315535)
      at Object.<anonymous>.require.Module._extensions (node_modules/@httptoolkit/esm/esm.js:1:163)
      at Object.Cp (node_modules/@httptoolkit/esm/esm.js:1:314277)
      at p (node_modules/@httptoolkit/esm/esm.js:1:315579)
      at cl (node_modules/@httptoolkit/esm/esm.js:1:275662)
      at Object.<anonymous> (node_modules/@httptoolkit/esm/esm.js:1:316224)
      at Object.<anonymous> (node_modules/@httptoolkit/esm/esm.js:1:257086)
      at Object.<anonymous>.require.Module._extensions (node_modules/@httptoolkit/esm/esm.js:1:163)
      at Object.Cp (node_modules/@httptoolkit/esm/esm.js:1:314277)
      at Object.u (node_modules/@httptoolkit/esm/esm.js:1:316406)
      at Object.o (node_modules/@httptoolkit/esm/esm.js:1:315227)
      at Object.<anonymous> (node_modules/@httptoolkit/esm/esm.js:1:312984)
      at Object.apply (node_modules/@httptoolkit/esm/esm.js:1:228557)
      at node_modules/@httptoolkit/esm/esm.js:1:279876
      at Object.<anonymous> (node_modules/@httptoolkit/esm/esm.js:1:279918)
      at node_modules/@httptoolkit/esm/esm.js:1:282340
      at node_modules/@httptoolkit/esm/esm.js:1:282416
      at vu (node_modules/@httptoolkit/esm/esm.js:1:257110)
      at Proxy.<anonymous> (node_modules/@httptoolkit/esm/esm.js:1:282190)
      at Object.<anonymous> (node_modules/@httptoolkit/esm/esm.js:1:280271)
      at Object.t (node_modules/@httptoolkit/esm/esm.js:1:307870)
      at n (node_modules/@httptoolkit/esm/esm.js:1:307684)
      at Object.<anonymous> (node_modules/js-yaml/lib/schema/default.js:11:18)
      at Object.<anonymous> (node_modules/@httptoolkit/esm/esm.js:1:279304)
      at Object.<anonymous> (node_modules/@httptoolkit/esm/esm.js:1:315535)
      at Object.<anonymous>.require.Module._extensions (node_modules/@httptoolkit/esm/esm.js:1:163)
      at Object.Cp (node_modules/@httptoolkit/esm/esm.js:1:314277)
      at p (node_modules/@httptoolkit/esm/esm.js:1:315579)
      at cl (node_modules/@httptoolkit/esm/esm.js:1:275662)
      at Object.<anonymous> (node_modules/@httptoolkit/esm/esm.js:1:316224)
      at Object.<anonymous> (node_modules/@httptoolkit/esm/esm.js:1:257086)
      at Object.<anonymous>.require.Module._extensions (node_modules/@httptoolkit/esm/esm.js:1:163)
      at Object.Cp (node_modules/@httptoolkit/esm/esm.js:1:314277)
      at Object.u (node_modules/@httptoolkit/esm/esm.js:1:316406)
      at Object.o (node_modules/@httptoolkit/esm/esm.js:1:315227)
      at Object.<anonymous> (node_modules/@httptoolkit/esm/esm.js:1:312984)
      at Object.apply (node_modules/@httptoolkit/esm/esm.js:1:228557)
      at node_modules/@httptoolkit/esm/esm.js:1:279876
      at Object.<anonymous> (node_modules/@httptoolkit/esm/esm.js:1:279918)
      at node_modules/@httptoolkit/esm/esm.js:1:282340
      at node_modules/@httptoolkit/esm/esm.js:1:282416
      at vu (node_modules/@httptoolkit/esm/esm.js:1:257110)
      at Proxy.<anonymous> (node_modules/@httptoolkit/esm/esm.js:1:282190)
      at Object.<anonymous> (node_modules/@httptoolkit/esm/esm.js:1:280271)
      at Object.t (node_modules/@httptoolkit/esm/esm.js:1:307870)
      at n (node_modules/@httptoolkit/esm/esm.js:1:307684)

      at esmImport (node_modules/import-sync/src/import.ts:18:11)
      at importSync (node_modules/import-sync/src/import.ts:40:26)
      at Object.importSync (src/k8s/kubernetesClient.js:5:13)
      at Object.require (src/k8s/documents.js:5:16)
      at Object.require (src/metadataClient.js:4:64)
      at Object.require (src/__tests__/metadataTest.js:6:24)

As far as I can tell from debug mode, type instanceof Type is resolving to false down inside a transient dependency js-yaml:

Image

Image

It is unclear why this might be the case.

robross0606 avatar Mar 12 '25 18:03 robross0606

Your image is a little low on resolution so I can't really see them.

A few questions

  1. Can you provide a minimal reproducible example as a gist or git repo?
  2. What is your node version, operating system, dependency versions, etc

Here's my attempt at reproducing it:

import-sync-kubernetes-client-node

nktnet1 avatar Mar 12 '25 23:03 nktnet1

I will create a minimal example, but also updated the images on my description to have better resolution.

Also, why does your example have tsx or typescript dev dependencies if it is just a minimal CommonJS project? As far as I can tell, your project isn't plain CommonJS at all if you're using typescript to transpile and run it.

robross0606 avatar Mar 12 '25 23:03 robross0606

What is your node version, operating system, dependency versions, etc

Node version: I tried with 18.20.6 and 22.14.0 OS: Microsoft Windows [Version 10.0.19045.5555] Dependencies:

  "dependencies": {
    "@kubernetes/client-node": "^1.0.0",
    "import-sync": "^2.2.3"
  },

robross0606 avatar Mar 12 '25 23:03 robross0606

I created a minimal example and it is just completely hanging on the call to importSync(). Running npm start hangs on that line.

index.js:

const importSync = require("import-sync");
const k8s = importSync("@kubernetes/client-node");

console.log(Object.keys(k8s));

package.json

{
  "name": "importsync-test",
  "version": "0.1.0",
  "description": "Minimal importsync test.",
  "main": "index.js",
  "scripts": {
    "test": "echo \"Error: no test specified\" && exit 1",
    "start": "node ./index.js"
  },
  "license": "ISC",
  "dependencies": {
    "@kubernetes/client-node": "^1.0.0",
    "import-sync": "^2.2.3"
  },
  "volta": {
    "node": "22.14.0"
  }
}

This uses nothing but vanilla Node.js 22.14.0. No typescript involved.

robross0606 avatar Mar 12 '25 23:03 robross0606

Turns out it wasn't hung at all. It just took a very long time to get past the importSync() line:

index.js

const { performance } = require("perf_hooks");

const importSync = require("import-sync");

const beforeTime = performance.now();
const k8s = importSync("@kubernetes/client-node");
const importTime = performance.now() - beforeTime;

console.log(`importSync in ${importTime / 1000} seconds.`);
console.log(Object.keys(k8s));

Output:

Node.js v22.14.0

C:\Code\public\importsync-test>npm start

> [email protected] start
> node ./index.js

importSync in 23.5274517 seconds.
[
  'KubeConfig',
  'makeAbsolutePath',
  'bufferFromFileOrString',
  'findHomeDir',
  'findObject',
...

23 seconds to load a module is far too slow to be usable if that is to be expected.

robross0606 avatar Mar 12 '25 23:03 robross0606

As far as the original issue description is concerned, I wonder if it is caused by the jest unit test environment somehow causing multiple module caches. I will try to add that to my minimal example and see if I can cause that particular problem.

robross0606 avatar Mar 12 '25 23:03 robross0606

As far as the original issue description is concerned, I wonder if it is caused by the jest unit test environment somehow causing multiple module caches. I will try to add that to my minimal example and see if I can cause that particular problem.

That is definitely the root incompatibility/cause. I added jest to the project:

package.json

{
  "name": "importsync-test",
  "version": "0.1.0",
  "description": "Minimal importsync test.",
  "main": "index.js",
  "scripts": {
    "test": "npx jest --coverage",
    "start": "node ./index.js"
  },
  "license": "ISC",
  "dependencies": {
    "@kubernetes/client-node": "^1.0.0",
    "import-sync": "^2.2.3"
  },
  "devDependencies": {
    "jest": "^29.7.0"
  },
  "jest": {
    "testEnvironment": "node"
  },
  "volta": {
    "node": "22.14.0"
  }
}

Then added index.test.js

test("Main entrant tests", () => {
  require("./index");
});

Output:

> npm test

> [email protected] test
> npx jest --coverage

 FAIL  ./index.test.js (21.539 s)
  ✕ Main entrant tests (16170 ms)

  ● Main entrant tests

    Failed to import from:
      @kubernetes/client-node
    Options:
      {}
    Require error message:
      YAMLException: Specified list of YAML types (or a single Type object) contains a non-Type object.

      4 |
      5 | const beforeTime = performance.now();
    > 6 | const k8s = importSync("@kubernetes/client-node");
        |             ^
      7 | const importTime = performance.now() - beforeTime;
      8 |
      9 | console.log(`importSync in ${importTime / 1000} seconds.`);

      at node_modules/js-yaml/lib/schema.js:104:13
          at Array.forEach (<anonymous>)
      at Schema.extend (node_modules/js-yaml/lib/schema.js:102:12)
      at new Schema (node_modules/js-yaml/lib/schema.js:62:15)
      at Object.<anonymous> (node_modules/js-yaml/lib/schema/failsafe.js:11:18)
      at Object.<anonymous> (node_modules/@httptoolkit/esm/esm.js:1:279304)
      at Object.<anonymous> (node_modules/@httptoolkit/esm/esm.js:1:315535)
      at Object.<anonymous>.require.Module._extensions (node_modules/@httptoolkit/esm/esm.js:1:163)
      at Object.Cp (node_modules/@httptoolkit/esm/esm.js:1:314277)
      at p (node_modules/@httptoolkit/esm/esm.js:1:315579)
      at cl (node_modules/@httptoolkit/esm/esm.js:1:275662)
      at Object.<anonymous> (node_modules/@httptoolkit/esm/esm.js:1:316224)
      at Object.<anonymous> (node_modules/@httptoolkit/esm/esm.js:1:257086)
      at Object.<anonymous>.require.Module._extensions (node_modules/@httptoolkit/esm/esm.js:1:163)
      at Object.Cp (node_modules/@httptoolkit/esm/esm.js:1:314277)
      at Object.u (node_modules/@httptoolkit/esm/esm.js:1:316406)
      at Object.o (node_modules/@httptoolkit/esm/esm.js:1:315227)
      at Object.<anonymous> (node_modules/@httptoolkit/esm/esm.js:1:312984)
      at Object.apply (node_modules/@httptoolkit/esm/esm.js:1:228557)
      at node_modules/@httptoolkit/esm/esm.js:1:279876
      at Object.<anonymous> (node_modules/@httptoolkit/esm/esm.js:1:279918)
      at node_modules/@httptoolkit/esm/esm.js:1:282340
      at node_modules/@httptoolkit/esm/esm.js:1:282416
      at vu (node_modules/@httptoolkit/esm/esm.js:1:257110)
      at Proxy.<anonymous> (node_modules/@httptoolkit/esm/esm.js:1:282190)
      at Object.<anonymous> (node_modules/@httptoolkit/esm/esm.js:1:280271)
      at Object.t (node_modules/@httptoolkit/esm/esm.js:1:307870)
      at n (node_modules/@httptoolkit/esm/esm.js:1:307684)
      at Object.<anonymous> (node_modules/js-yaml/lib/schema/json.js:12:18)
      at Object.<anonymous> (node_modules/@httptoolkit/esm/esm.js:1:279304)
      at Object.<anonymous> (node_modules/@httptoolkit/esm/esm.js:1:315535)
      at Object.<anonymous>.require.Module._extensions (node_modules/@httptoolkit/esm/esm.js:1:163)
      at Object.Cp (node_modules/@httptoolkit/esm/esm.js:1:314277)
      at p (node_modules/@httptoolkit/esm/esm.js:1:315579)
      at cl (node_modules/@httptoolkit/esm/esm.js:1:275662)
      at Object.<anonymous> (node_modules/@httptoolkit/esm/esm.js:1:316224)
      at Object.<anonymous> (node_modules/@httptoolkit/esm/esm.js:1:257086)
      at Object.<anonymous>.require.Module._extensions (node_modules/@httptoolkit/esm/esm.js:1:163)
      at Object.Cp (node_modules/@httptoolkit/esm/esm.js:1:314277)
      at Object.u (node_modules/@httptoolkit/esm/esm.js:1:316406)
      at Object.o (node_modules/@httptoolkit/esm/esm.js:1:315227)
      at Object.<anonymous> (node_modules/@httptoolkit/esm/esm.js:1:312984)
      at Object.apply (node_modules/@httptoolkit/esm/esm.js:1:228557)
      at node_modules/@httptoolkit/esm/esm.js:1:279876
      at Object.<anonymous> (node_modules/@httptoolkit/esm/esm.js:1:279918)
      at node_modules/@httptoolkit/esm/esm.js:1:282340
      at node_modules/@httptoolkit/esm/esm.js:1:282416
      at vu (node_modules/@httptoolkit/esm/esm.js:1:257110)
      at Proxy.<anonymous> (node_modules/@httptoolkit/esm/esm.js:1:282190)
      at Object.<anonymous> (node_modules/@httptoolkit/esm/esm.js:1:280271)
      at Object.t (node_modules/@httptoolkit/esm/esm.js:1:307870)
      at n (node_modules/@httptoolkit/esm/esm.js:1:307684)
      at Object.<anonymous> (node_modules/js-yaml/lib/schema/core.js:11:18)
      at Object.<anonymous> (node_modules/@httptoolkit/esm/esm.js:1:279304)
      at Object.<anonymous> (node_modules/@httptoolkit/esm/esm.js:1:315535)
      at Object.<anonymous>.require.Module._extensions (node_modules/@httptoolkit/esm/esm.js:1:163)
      at Object.Cp (node_modules/@httptoolkit/esm/esm.js:1:314277)
      at p (node_modules/@httptoolkit/esm/esm.js:1:315579)
      at cl (node_modules/@httptoolkit/esm/esm.js:1:275662)
      at Object.<anonymous> (node_modules/@httptoolkit/esm/esm.js:1:316224)
      at Object.<anonymous> (node_modules/@httptoolkit/esm/esm.js:1:257086)
      at Object.<anonymous>.require.Module._extensions (node_modules/@httptoolkit/esm/esm.js:1:163)
      at Object.Cp (node_modules/@httptoolkit/esm/esm.js:1:314277)
      at Object.u (node_modules/@httptoolkit/esm/esm.js:1:316406)
      at Object.o (node_modules/@httptoolkit/esm/esm.js:1:315227)
      at Object.<anonymous> (node_modules/@httptoolkit/esm/esm.js:1:312984)
      at Object.apply (node_modules/@httptoolkit/esm/esm.js:1:228557)
      at node_modules/@httptoolkit/esm/esm.js:1:279876
      at Object.<anonymous> (node_modules/@httptoolkit/esm/esm.js:1:279918)
      at node_modules/@httptoolkit/esm/esm.js:1:282340
      at node_modules/@httptoolkit/esm/esm.js:1:282416
      at vu (node_modules/@httptoolkit/esm/esm.js:1:257110)
      at Proxy.<anonymous> (node_modules/@httptoolkit/esm/esm.js:1:282190)
      at Object.<anonymous> (node_modules/@httptoolkit/esm/esm.js:1:280271)
      at Object.t (node_modules/@httptoolkit/esm/esm.js:1:307870)
      at n (node_modules/@httptoolkit/esm/esm.js:1:307684)
      at Object.<anonymous> (node_modules/js-yaml/lib/schema/default.js:11:18)
      at Object.<anonymous> (node_modules/@httptoolkit/esm/esm.js:1:279304)
      at Object.<anonymous> (node_modules/@httptoolkit/esm/esm.js:1:315535)
      at Object.<anonymous>.require.Module._extensions (node_modules/@httptoolkit/esm/esm.js:1:163)
      at Object.Cp (node_modules/@httptoolkit/esm/esm.js:1:314277)
      at p (node_modules/@httptoolkit/esm/esm.js:1:315579)
      at cl (node_modules/@httptoolkit/esm/esm.js:1:275662)
      at Object.<anonymous> (node_modules/@httptoolkit/esm/esm.js:1:316224)
      at Object.<anonymous> (node_modules/@httptoolkit/esm/esm.js:1:257086)
      at Object.<anonymous>.require.Module._extensions (node_modules/@httptoolkit/esm/esm.js:1:163)
      at Object.Cp (node_modules/@httptoolkit/esm/esm.js:1:314277)
      at Object.u (node_modules/@httptoolkit/esm/esm.js:1:316406)
      at Object.o (node_modules/@httptoolkit/esm/esm.js:1:315227)
      at Object.<anonymous> (node_modules/@httptoolkit/esm/esm.js:1:312984)
      at Object.apply (node_modules/@httptoolkit/esm/esm.js:1:228557)
      at node_modules/@httptoolkit/esm/esm.js:1:279876
      at Object.<anonymous> (node_modules/@httptoolkit/esm/esm.js:1:279918)
      at node_modules/@httptoolkit/esm/esm.js:1:282340
      at node_modules/@httptoolkit/esm/esm.js:1:282416
      at vu (node_modules/@httptoolkit/esm/esm.js:1:257110)
      at Proxy.<anonymous> (node_modules/@httptoolkit/esm/esm.js:1:282190)
      at Object.<anonymous> (node_modules/@httptoolkit/esm/esm.js:1:280271)
      at Object.t (node_modules/@httptoolkit/esm/esm.js:1:307870)
      at n (node_modules/@httptoolkit/esm/esm.js:1:307684)

      at esmImport (node_modules/import-sync/src/import.ts:18:11)
      at importSync (node_modules/import-sync/src/import.ts:40:26)
      at Object.importSync (index.js:6:13)
      at Object.require (index.test.js:2:3)

----------|---------|----------|---------|---------|-------------------
File      | % Stmts | % Branch | % Funcs | % Lines | Uncovered Line #s 
----------|---------|----------|---------|---------|-------------------
All files |   57.14 |      100 |     100 |   57.14 | 
 index.js |   57.14 |      100 |     100 |   57.14 | 7-10
----------|---------|----------|---------|---------|-------------------
Test Suites: 1 failed, 1 total
Tests:       1 failed, 1 total
Snapshots:   0 total
Time:        22.813 s, estimated 23 s
Ran all test suites.

robross0606 avatar Mar 13 '25 00:03 robross0606

It takes around 5-6 seconds for me, but yeah that is slow.

I've uploaded your reproducible example here:

  • https://github.com/nktnet-bug-report/import-sync-138

If you're on the latest version of node, you'll be able to import it without needing this library (see #113).

If you wish to pursue it further, I suggest opening an issue in

  • https://github.com/httptoolkit/esm

directly, as import-sync uses it under the hood.

nktnet1 avatar Mar 15 '25 14:03 nktnet1

If you're on the latest version of node, you'll be able to import it without needing this library (see https://github.com/nktnet1/import-sync/issues/113).

Is this true? I see it listed as a release candidate and experimental even on Node.js v23.11.0.

robross0606 avatar Apr 02 '25 14:04 robross0606