Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

TypeError: Cannot read properties of undefined (reading 'imports') #6832

Closed
6 tasks done
unional opened this issue Oct 31, 2024 · 7 comments · Fixed by #6811
Closed
6 tasks done

TypeError: Cannot read properties of undefined (reading 'imports') #6832

unional opened this issue Oct 31, 2024 · 7 comments · Fixed by #6811

Comments

@unional
Copy link

unional commented Oct 31, 2024

Describe the bug

When running vitest on https://github.com/repobuddy/storybook-addon-vis/tree/und-imports,
I notice it emits two warnings:

``sh
10:39:13 AM [vite] ✨ new dependencies optimized: @vitest/coverage-v8/browser
10:39:13 AM [vite] ✨ optimized dependencies changed. reloading

[vitest] Vite unexpectedly reloaded a test. This may cause tests to fail, lead to flaky behaviour or duplicated test runs.
For a stable experience, please add mentioned dependencies to your config's optimizeDeps.include field manually.

...

10:39:15 AM [vite] ✨ new dependencies optimized: @storybook/experimental-addon-test/internal/test-utils
10:39:15 AM [vite] ✨ optimized dependencies changed. reloading
[vitest] Vite unexpectedly reloaded a test. This may cause tests to fail, lead to flaky behaviour or duplicated test runs.
For a stable experience, please add mentioned dependencies to your config's optimizeDeps.include field manually.


So I follow exactly what it suggest:

optimizeDeps: {
	include: ['@storybook/experimental-addon-test/internal/test-utils', '@vitest/coverage-v8'],
},

I starts getting this error both locally (WSL and mac):

```sh
TypeError: Cannot read properties of undefined (reading 'imports')
 ❯ node_modules/.pnpm/vite@5.4.10_@types+node@22.8.1/node_modules/vite/dist/node/chunks/dep-BWSbWtLw.js:50832:71
 ❯ process.processTicksAndRejections node:internal/process/task_queues:105:5

Reproduction

https://github.com/repobuddy/storybook-addon-vis/tree/und-imports

pnpm i
pnpm verify

System Info

System:
    OS: macOS 14.7
    CPU: (10) arm64 Apple M1 Pro
    Memory: 95.86 MB / 16.00 GB
    Shell: 5.9 - /bin/zsh
  Binaries:
    Node: 22.9.0 - ~/.local/state/fnm_multishells/62333_1730395978755/bin/node
    npm: 10.8.3 - ~/.local/state/fnm_multishells/62333_1730395978755/bin/npm
    pnpm: 9.12.3 - ~/.nvm/versions/node/v20.12.2/bin/pnpm
    bun: 0.1.6 - ~/.bun/bin/bun
  Browsers:
    Brave Browser: 129.1.70.119
    Chrome: 130.0.6723.91
    Safari: 18.0
  npmPackages:
    @vitest/coverage-v8: 2.1.4 => 2.1.4 
    vitest: ^2.1.3 => 2.1.4

Used Package Manager

pnpm

Validations

unional added a commit to repobuddy/storybook-addon-vis that referenced this issue Oct 31, 2024
@unional
Copy link
Author

unional commented Oct 31, 2024

https://github.com/repobuddy/storybook-addon-vis/actions/runs/11618923903/job/32357403971

Note that sometimes it still fail even without opitmizeDeps

@unional
Copy link
Author

unional commented Nov 3, 2024

I add some logs to it, it fails at:

 const output = esbuildOutputFromId(
   meta.outputs,
   id,
   processingCacheDir
 ;
 ...
 JSON.stringify(output.imports) // here
  • id: react
  • processingCacheDir: /home/runner/work/storybook-addon-vis/storybook-addon-vis/testcases/react-vite-ts/node_modules/.vite/deps_temp_3433dfbb
  • meta.outputs:
{
  '../../testcases/react-vite-ts/node_modules/.vite/deps_temp_3433dfbb/pixelmatch.js.map': { imports: [], exports: [], inputs: {}, bytes: 13902 },
  '../../testcases/react-vite-ts/node_modules/.vite/deps_temp_3433dfbb/pixelmatch.js': {
    imports: [ [Object] ],
    exports: [ 'default' ],
    entryPoint: '../../node_modules/.pnpm/pixelmatch@6.0.0/node_modules/pixelmatch/index.js',
    inputs: {
      '../../node_modules/.pnpm/pixelmatch@6.0.0/node_modules/pixelmatch/index.js': [Object]
    },
    bytes: 6014
  },
  '../../testcases/react-vite-ts/node_modules/.vite/deps_temp_3433dfbb/storybook_internal_channels.js.map': { imports: [], exports: [], inputs: {}, bytes: 93 },
  '../../testcases/react-vite-ts/node_modules/.vite/deps_temp_3433dfbb/storybook_internal_channels.js': {
    imports: [ [Object], [Object] ],
    exports: [
      'Channel',
      'PostMessageTransport',
      'WebsocketTransport',
      'createBrowserChannel'
    ],
    entryPoint: '../../node_modules/.pnpm/storybook@8.3.6/node_modules/storybook/core/channels/index.js',
    inputs: {
      '../../node_modules/.pnpm/storybook@8.3.6/node_modules/storybook/core/channels/index.js': [Object]
    },
    bytes: 270
  },
  '../../testcases/react-vite-ts/node_modules/.vite/deps_temp_3433dfbb/@storybook_test.js.map': { imports: [], exports: [], inputs: {}, bytes: 1527003 },
  '../../testcases/react-vite-ts/node_modules/.vite/deps_temp_3433dfbb/@storybook_test.js': {
    imports: [ [Object], [Object], [Object] ],
    exports: [
      'buildQueries',
      'clearAllMocks',
      'configure',
      'createEvent',
      'expect',
      'findAllByAltText',
      'findAllByDisplayValue',
      'custom',
      'inspect',
      'lib_default',
      'registerConstructor',
      'registerStringTag'
    ],
    inputs: {
      'browser-external:util': [Object],
      '../../node_modules/.pnpm/loupe@3.1.2/node_modules/loupe/lib/helpers.js': [Object],
      '../../node_modules/.pnpm/loupe@3.1.2/node_modules/loupe/lib/array.js': [Object],
      '../../node_modules/.pnpm/loupe@3.1.2/node_modules/loupe/lib/typedarray.js': [Object],
      '../../node_modules/.pnpm/loupe@3.1.2/node_modules/loupe/lib/date.js': [Object],
      '../../node_modules/.pnpm/loupe@3.1.2/node_modules/loupe/lib/function.js': [Object],
      '../../node_modules/.pnpm/loupe@3.1.2/node_modules/loupe/lib/map.js': [Object],
      '../../node_modules/.pnpm/loupe@3.1.2/node_modules/loupe/lib/number.js': [Object],
      '../../node_modules/.pnpm/loupe@3.1.2/node_modules/loupe/lib/bigint.js': [Object],
      '../../node_modules/.pnpm/loupe@3.1.2/node_modules/loupe/lib/regexp.js': [Object],
      '../../node_modules/.pnpm/loupe@3.1.2/node_modules/loupe/lib/set.js': [Object],
      '../../node_modules/.pnpm/loupe@3.1.2/node_modules/loupe/lib/string.js': [Object],
      '../../node_modules/.pnpm/loupe@3.1.2/node_modules/loupe/lib/symbol.js': [Object],
      '../../node_modules/.pnpm/loupe@3.1.2/node_modules/loupe/lib/promise.js': [Object],
      '../../node_modules/.pnpm/loupe@3.1.2/node_modules/loupe/lib/object.js': [Object],
      '../../node_modules/.pnpm/loupe@3.1.2/node_modules/loupe/lib/class.js': [Object],
      '../../node_modules/.pnpm/loupe@3.1.2/node_modules/loupe/lib/arguments.js': [Object],
      '../../node_modules/.pnpm/loupe@3.1.2/node_modules/loupe/lib/error.js': [Object],
      '../../node_modules/.pnpm/loupe@3.1.2/node_modules/loupe/lib/html.js': [Object],
      '../../node_modules/.pnpm/loupe@3.1.2/node_modules/loupe/lib/index.js': [Object]
    },
    bytes: 20074
  },
  '../../testcases/react-vite-ts/node_modules/.vite/deps_temp_3433dfbb/chunk-2TUXWMP5.js.map': { imports: [], exports: [], inputs: {}, bytes: 93 },
  '../../testcases/react-vite-ts/node_modules/.vite/deps_temp_3433dfbb/chunk-2TUXWMP5.js': {
    imports: [],
    exports: [
      '__commonJS',
      '__export',
      '__publicField',
      '__require',
      '__toESM'
    ],
    inputs: {},
    bytes: 2209
  }
}

https://github.com/repobuddy/storybook-addon-vis/actions/runs/11654315823/job/32447708042

@unional
Copy link
Author

unional commented Nov 3, 2024

I changed the logs to better show what's the problem:

Trace: esbuildOutputFromId cannot locate output: react
cwd: /home/runner/work/storybook-addon-vis/storybook-addon-vis
cacheDirOutputPath: /home/runner/work/storybook-addon-vis/storybook-addon-vis/testcases/react-vite-ts/node_modules/.vite/deps_temp_96249d16
normalizedOutputPath: testcases/react-vite-ts/node_modules/.vite/deps_temp_96249d16/react.js
testPaths: ../../testcases/react-vite-ts/node_modules/.vite/deps_temp_96249d16/pixelmatch.js.map
../../testcases/react-vite-ts/node_modules/.vite/deps_temp_96249d16/pixelmatch.js
../../testcases/react-vite-ts/node_modules/.vite/deps_temp_96249d16/storybook_internal_channels.js.map
../../testcases/react-vite-ts/node_modules/.vite/deps_temp_96249d16/storybook_internal_channels.js
../../testcases/react-vite-ts/node_modules/.vite/deps_temp_96249d16/@storybook_test.js.map
../../testcases/react-vite-ts/node_modules/.vite/deps_temp_96249d16/@storybook_test.js
../../testcases/react-vite-ts/node_modules/.vite/deps_temp_96249d16/react-18-N3LFYOCR.js.map
../../testcases/react-vite-ts/node_modules/.vite/deps_temp_96249d16/react-18-N3LFYOCR.js
../../testcases/react-vite-ts/node_modules/.vite/deps_temp_96249d16/vitest___@vitest_utils___loupe.js.map
../../testcases/react-vite-ts/node_modules/.vite/deps_temp_96249d16/vitest___@vitest_utils___loupe.js
../../testcases/react-vite-ts/node_modules/.vite/deps_temp_96249d16/@vitest_browser___@testing-library_user-event.js.map
../../testcases/react-vite-ts/node_modules/.vite/deps_temp_96249d16/@vitest_browser___@testing-library_user-event.js
../../testcases/react-vite-ts/node_modules/.vite/deps_temp_96249d16/@vitest_browser___@testing-library_dom.js.map
../../testcases/react-vite-ts/node_modules/.vite/deps_temp_96249d16/@vitest_browser___@testing-library_dom.js
../../testcases/react-vite-ts/node_modules/.vite/deps_temp_96249d16/@storybook_react.js.map
../../testcases/react-vite-ts/node_modules/.vite/deps_temp_96249d16/@storybook_react.js
../../testcases/react-vite-ts/node_modules/.vite/deps_temp_96249d16/chunk-SIYMET6G.js.map
../../testcases/react-vite-ts/node_modules/.vite/deps_temp_96249d16/chunk-SIYMET6G.js
../../testcases/react-vite-ts/node_modules/.vite/deps_temp_96249d16/chunk-KS2XOV6Z.js.map
../../testcases/react-vite-ts/node_modules/.vite/deps_temp_96249d16/chunk-KS2XOV6Z.js
../../testcases/react-vite-ts/node_modules/.vite/deps_temp_96249d16/@testing-library_react.js.map
../../testcases/react-vite-ts/node_modules/.vite/deps_temp_96249d16/@testing-library_react.js
../../testcases/react-vite-ts/node_modules/.vite/deps_temp_96249d16/chunk-JCWZPSOH.js.map
../../testcases/react-vite-ts/node_modules/.vite/deps_temp_96249d16/chunk-JCWZPSOH.js
../../testcases/react-vite-ts/node_modules/.vite/deps_temp_96249d16/chunk-R4Q7MLCB.js.map
../../testcases/react-vite-ts/node_modules/.vite/deps_temp_96249d16/chunk-R4Q7MLCB.js
../../testcases/react-vite-ts/node_modules/.vite/deps_temp_96249d16/chunk-M6PHUQY3.js.map
../../testcases/react-vite-ts/node_modules/.vite/deps_temp_96249d16/chunk-M6PHUQY3.js
../../testcases/react-vite-ts/node_modules/.vite/deps_temp_96249d16/chai.js.map
../../testcases/react-vite-ts/node_modules/.vite/deps_temp_96249d16/chai.js
../../testcases/react-vite-ts/node_modules/.vite/deps_temp_96249d16/dedent.js.map
../../testcases/react-vite-ts/node_modules/.vite/deps_temp_96249d16/dedent.js
../../testcases/react-vite-ts/node_modules/.vite/deps_temp_96249d16/expect-type.js.map
../../testcases/react-vite-ts/node_modules/.vite/deps_temp_96249d16/expect-type.js
../../testcases/react-vite-ts/node_modules/.vite/deps_temp_96249d16/react.js.map
../../testcases/react-vite-ts/node_modules/.vite/deps_temp_96249d16/react.js
../../testcases/react-vite-ts/node_modules/.vite/deps_temp_96249d16/react-dom.js.map
../../testcases/react-vite-ts/node_modules/.vite/deps_temp_96249d16/react-dom.js
../../testcases/react-vite-ts/node_modules/.vite/deps_temp_96249d16/chunk-XMNNGJFR.js.map
../../testcases/react-vite-ts/node_modules/.vite/deps_temp_96249d16/chunk-XMNNGJFR.js
../../testcases/react-vite-ts/node_modules/.vite/deps_temp_96249d16/react_jsx-dev-runtime.js.map
../../testcases/react-vite-ts/node_modules/.vite/deps_temp_96249d16/react_jsx-dev-runtime.js
../../testcases/react-vite-ts/node_modules/.vite/deps_temp_96249d16/react_jsx-runtime.js.map
../../testcases/react-vite-ts/node_modules/.vite/deps_temp_96249d16/react_jsx-runtime.js
../../testcases/react-vite-ts/node_modules/.vite/deps_temp_96249d16/chunk-4DCPJHYI.js.map
../../testcases/react-vite-ts/node_modules/.vite/deps_temp_96249d16/chunk-4DCPJHYI.js
../../testcases/react-vite-ts/node_modules/.vite/deps_temp_96249d16/vitest___expect-type.js.map
../../testcases/react-vite-ts/node_modules/.vite/deps_temp_96249d16/vitest___expect-type.js
../../testcases/react-vite-ts/node_modules/.vite/deps_temp_96249d16/chunk-ZYKTUFVC.js.map
../../testcases/react-vite-ts/node_modules/.vite/deps_temp_96249d16/chunk-ZYKTUFVC.js
../../testcases/react-vite-ts/node_modules/.vite/deps_temp_96249d16/vitest___@vitest_snapshot___magic-string.js.map
../../testcases/react-vite-ts/node_modules/.vite/deps_temp_96249d16/vitest___@vitest_snapshot___magic-string.js
../../testcases/react-vite-ts/node_modules/.vite/deps_temp_96249d16/vitest___chai.js.map
../../testcases/react-vite-ts/node_modules/.vite/deps_temp_96249d16/vitest___chai.js
../../testcases/react-vite-ts/node_modules/.vite/deps_temp_96249d16/chunk-AOBQ2QTI.js.map
../../testcases/react-vite-ts/node_modules/.vite/deps_temp_96249d16/chunk-AOBQ2QTI.js
../../testcases/react-vite-ts/node_modules/.vite/deps_temp_96249d16/vitest___chai___loupe.js.map
../../testcases/react-vite-ts/node_modules/.vite/deps_temp_96249d16/vitest___chai___loupe.js
../../testcases/react-vite-ts/node_modules/.vite/deps_temp_96249d16/chunk-CO7AV33B.js.map
../../testcases/react-vite-ts/node_modules/.vite/deps_temp_96249d16/chunk-CO7AV33B.js
../../testcases/react-vite-ts/node_modules/.vite/deps_temp_96249d16/chunk-2TUXWMP5.js.map
../../testcases/react-vite-ts/node_modules/.vite/deps_temp_96249d16/chunk-2TUXWMP5.js

    at esbuildOutputFromId (file:///home/runner/work/storybook-addon-vis/storybook-addon-vis/node_modules/.pnpm/vite@5.4.10_patch_hash=yxgidkvfiyjalv7bn63o44e6y4_@types+node@22.8.1/node_modules/vite/dist/node/chunks/dep-BWSbWtLw.js:5[113](https://github.com/repobuddy/storybook-addon-vis/actions/runs/11654506689/job/32448111859#step:9:116)6:10)
    at file:///home/runner/work/storybook-addon-vis/storybook-addon-vis/node_modules/.pnpm/vite@5.4.10_patch_hash=yxgidkvfiyjalv7bn63o44e6y4_@types+node@22.8.1/node_modules/vite/dist/node/chunks/dep-BWSbWtLw.js:50821:24
    at process.processTicksAndRejections (node:internal/process/task_queues:105:5)

So the problems is the cwd is the root of the monorepo instead of the root of the project (./testcases/react-vite-ts in this case).

@unional unional changed the title optimizeDeps causes TypeError: Cannot read properties of undefined (reading 'imports') TypeError: Cannot read properties of undefined (reading 'imports') Nov 3, 2024
@unional
Copy link
Author

unional commented Nov 3, 2024

Here is my patched code to emit the logs:

function esbuildOutputFromId(outputs, id, cacheDirOutputPath) {
  const cwd = process.cwd();
  const flatId = flattenId(id) + ".js";
  const normalizedOutputPath = normalizePath$3(
    path$n.relative(cwd, path$n.join(cacheDirOutputPath, flatId))
  );
  const output = outputs[normalizedOutputPath];
  if (output) {
    return output;
  }
  const testPaths = []
  for (const [key, value] of Object.entries(outputs)) {
    const testPath = normalizePath$3(path$n.relative(cwd, key))
    testPaths.push(testPath)
    if (testPath === normalizedOutputPath) {
      return value;
    }
  }
  console.trace(`esbuildOutputFromId cannot locate output: ${id}
cwd: ${cwd}
cacheDirOutputPath: ${cacheDirOutputPath}
normalizedOutputPath: ${normalizedOutputPath}
testPaths: ${testPaths.join('\n')}
`);
}

@sheremet-va
Copy link
Member

sheremet-va commented Nov 3, 2024

I add some logs to it, it fails at:

This is not Vitest code, this is an error in Vite. But it's possible the error happens because of how workspace projects are loaded. #6811 should fix it

@unional
Copy link
Author

unional commented Nov 3, 2024

Note that the failure does not happen all the times. I think the failure rate is about 10% in CI and less than 2% locally.

@unional
Copy link
Author

unional commented Nov 3, 2024

IMO the problem is related to shared caching.
I just encounter a case locally that the error always show up in ./packages/vis.

I couldn't get it to work even I remove node_modules and start over.

To "fix" it, instead of running vitest in workspace mode at the root,
I run it in the project once (pnpm vis test, essentially cd packages/vis && pnpm test).
After that, running vitest at the root is working again.

@github-actions github-actions bot locked and limited conversation to collaborators Nov 20, 2024
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants