You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
bitdev.react/react-env generated environments fail to build when vitest.config is converted from .mjs to .ts due to hardcoded TypeScript moduleResolution #10504
When creating a React environment using bitdev.react/react-env, changing vitest.config.mjs to vitest.config.ts causes bit build to fail while compiling bit-env. The error suggests that the TypeScript compiler is using an incompatible moduleResolution setting (node instead of node16, nodenext, or bundler) despite the required dependencies being present.
This indicates that bit-env uses a hardcoded TypeScript configuration during compilation which doesn't allow packages such as vite and @vitejs/plugin-react to be resolved correctly.
This is especially surprising given the workarounds and fixes around TypeScript configuration discussed in #10502.
Steps to Reproduce
bit create react-env envs/my-react-env --aspect bitdev.react/react-env
bit create react-app apps/react-frontend --env projects/envs/my-react-env
After creation, the generated environment has the following type:
bitdev.general/envs/bit-env@5.0.12
For comparison, manually changing the env with:
bit env set projects/commonlib/envs/my-react-env bitdev.react/react-env
causes the env var to detach and reports that bitdev.react/react-env is not of type env. I assume this behavior is intentional and simply differs from how @teambit/react-env behaved previously.
Now, rename:
vitest.config.mjs
to:
vitest.config.ts
point <env>.bit.env.ts to it, add types to your config so it's compatible with TS and run:
bit build
Actual Behavior
bit build fails during the bit-env compilation step with errors such as:
error TS2307: Cannot find module 'vite' or its corresponding type declarations.
There are types at:
.../node_modules/.../vite/dist/node/index.d.ts
but this result could not be resolved under your current 'moduleResolution' setting.
Consider updating to 'node16', 'nodenext', or 'bundler'.
The same error is emitted for @vitejs/plugin-react as well as for any custom module augmentations for vite.
The build output shows that the failure happens during the bit-env compilation process:
(envs/bit-env) [Compiler: TypescriptCompile]
Expected Behavior
vitest.config.ts should compile successfully when using modern ESM packages such as vite and @vitejs/plugin-react, or the generated environment should inherit/support the appropriate TypeScript moduleResolution setting (node16, nodenext, or bundler).
At the very least, the TypeScript configuration used internally by bit-env should be configurable or should not force an incompatible moduleResolution setting.
Additional Notes
The required type declarations are present in node_modules.
TypeScript explicitly reports that changing moduleResolution would resolve the imports.
The issue only manifests once vitest.config is written in TypeScript.
This completely blocks bit build.
It is a regression from @teambit/react.react-env as in Bit v1.13, I kept using TS based configs without issues.
if you import a local module like: import { toPosixPath } from "./my-typescript-config-writer"; without providing an extension, VS Code will be fine with it but trying to run any bit command (even bit ws-config write) will tell you (and rightly so) that the env var became detached ⚠ environment with ID: projects/commonlib/envs/my-react-env configured on component projects/commonlib/components/my-css-grid was not loaded (run "bit install"). It is detached because bit-env hard coded compilerOptions (similarly to the case of moduleResolution: node I described before) break it at (bit) runtime.
since exclude values are not respected (not written), bit compile would take 7 minutes because it doesn't know to skip **/prod-build-electron/** which can contain an unpacked asar needed for debugging purposes. This also propagates to capsules during bit build and so on.
Workaround
I'm currently using a custom vite.d.ts file with ambient module declarations for vite and @vitejs/plugin-react in <my-env>/types to circumvent these compilatiuon issues. This way, when the parent bit-env's compiler runs, TypeScript will find the local .d.ts declarations instead of trying to resolve the actual vite package through the incorrectly forced node moduleResolution strategy.
Summary
When creating a React environment using
bitdev.react/react-env, changingvitest.config.mjstovitest.config.tscausesbit buildto fail while compilingbit-env. The error suggests that the TypeScript compiler is using an incompatiblemoduleResolutionsetting (nodeinstead ofnode16,nodenext, orbundler) despite the required dependencies being present.This indicates that
bit-envuses a hardcoded TypeScript configuration during compilation which doesn't allow packages such asviteand@vitejs/plugin-reactto be resolved correctly.This is especially surprising given the workarounds and fixes around TypeScript configuration discussed in #10502.
Steps to Reproduce
After creation, the generated environment has the following type:
For comparison, manually changing the env with:
bit env set projects/commonlib/envs/my-react-env bitdev.react/react-envcauses the env var to detach and reports that
bitdev.react/react-envis not of type env. I assume this behavior is intentional and simply differs from how@teambit/react-envbehaved previously.Now, rename:
to:
point
<env>.bit.env.tsto it, add types to your config so it's compatible with TS and run:Actual Behavior
bit buildfails during thebit-envcompilation step with errors such as:The same error is emitted for
@vitejs/plugin-reactas well as for any custom module augmentations forvite.The failures occur while compiling files such as:
The build output shows that the failure happens during the
bit-envcompilation process:Expected Behavior
vitest.config.tsshould compile successfully when using modern ESM packages such asviteand@vitejs/plugin-react, or the generated environment should inherit/support the appropriate TypeScriptmoduleResolutionsetting (node16,nodenext, orbundler).At the very least, the TypeScript configuration used internally by
bit-envshould be configurable or should not force an incompatiblemoduleResolutionsetting.Additional Notes
node_modules.moduleResolutionwould resolve the imports.vitest.configis written in TypeScript.bit build.@teambit/react.react-envas in Bit v1.13, I kept using TS based configs without issues.ConfigWriterListpipeline which ignores provided options #10502), also cause weird, difficult to diagnose quirks, i.e.:import { toPosixPath } from "./my-typescript-config-writer";without providing an extension, VS Code will be fine with it but trying to run any bit command (evenbit ws-config write) will tell you (and rightly so) that the env var became detached⚠ environment with ID: projects/commonlib/envs/my-react-env configured on component projects/commonlib/components/my-css-grid was not loaded (run "bit install"). It is detached becausebit-envhard codedcompilerOptions(similarly to the case ofmoduleResolution: nodeI described before) break it at (bit) runtime.excludevalues are not respected (not written),bit compilewould take 7 minutes because it doesn't know to skip**/prod-build-electron/**which can contain an unpacked asar needed for debugging purposes. This also propagates to capsules duringbit buildand so on.Workaround
I'm currently using a custom
vite.d.tsfile with ambient module declarations forviteand@vitejs/plugin-reactin<my-env>/typesto circumvent these compilatiuon issues. This way, when the parent bit-env's compiler runs, TypeScript will find the local.d.tsdeclarations instead of trying to resolve the actualvitepackage through the incorrectly forcednodemoduleResolution strategy.I have also updated the status here with new findings: #10480 (comment). I don't have permissions to reopen the issue as you asked.