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

Inconsistent typescript rules between custom environment compiler and build task #8889

Open
sbland opened this issue May 15, 2024 · 8 comments
Labels

Comments

@sbland
Copy link

sbland commented May 15, 2024

Describe the bug

I have a custom react environment which mostly inherited from @bitdev/react.react-env.

When I run bit check-types or bit compile it has no errors but when I run bit snap -b I get typescript errors. It appears that the check-types function is not running in strict mode but the build pipeline is.

I.e. the compile pipeline doesn't enforce implicit any so the following code pases

function hello(foo)  {
  console.info(foo)
}

but with the build pipeline I would get the error error TS7006: Parameter 'foo' implicitly has an 'any' type.

The tsconfig.json looks like this:

{
  "extends": "@teambit/react.react-env/config/tsconfig.json",
  "exclude": ["artifacts", "public", "dist", "node_modules", "package.json", "**/*.cjs"],
  "include": ["**/*", "**/*.json"]
}

.bit.env.ts file

...

  compiler(): EnvHandler<Compiler> {
    return TypescriptCompiler.from({
      tsconfig: this.tsconfigPath,
      types: resolveTypes(__dirname, [this.tsTypesPath]),
      typescript,
    });
  }


  build() {
    return Pipeline.from([
      TypescriptTask.from({
        tsconfig: this.tsconfigPath,
        types: resolveTypes(__dirname, [this.tsTypesPath]),
        typescript,
      }),
      EslintTask.from({
        eslint: ESLintLib,
        tsconfig: this.tsconfigPath,
        configPath: this.eslintConfigPath,
        pluginsPath: __dirname,
        extensions: this.eslintExtensions,
      }),
      VitestTask.from({
        config: require.resolve('./config/vitest.config.mjs'),
      }),
    ]);
  }

Expected Behavior

bit check-types should fail as strict: true is in the tsconfig

Specifications

  • Bit version:1.6.145
  • Workspace type: harmony/
  • Node version: v18.19.1
  • npm / yarn version: 10.2.4
  • Platform: ubuntu

for harmony workspace

  • relevant env @bitdev/react.react-env.
@GiladShoham
Copy link
Member

can you try running
bit ws-config write --clean
then the check-types again

@sbland
Copy link
Author

sbland commented May 15, 2024

Thanks @GiladShoham I ran that and it added a few tsconfig and eslint files.
check-types still returned no errors.
When I dug into some of the tsconfigs created i noticed that it was actually my node env compnents that were not working as expected. For background info I have 2 custom environments in my workspace.

  • One for node components (which inherits @bitdev/node.node-env)
  • one for React components (which inherits @bitdev/react.react-env).

I noticed that tsconfig files were created in any directory that only contained the components with the custom node env. These tsconfig files appear to not be importing my custom tsconfig.
I then compared the .bit.env files for each environment and noticed in the React environment I had defined protected tsconfigPath = require.resolve('./config/tsconfig.json'); but I hadn't in the node environment.
Adding this seems to have resolved the issue but it seems odd that without this the tsconfig was different between compile and build even though both referred to this.tsconfigPath

@sbland
Copy link
Author

sbland commented May 16, 2024

I now get the correct type checking in VSCode but check-types is still not picking up on the correct tsconfig.
I've tried the following and none give me any type errors from check-types:

  • bit reset --all then bit install then bit check-types (no errors) bit snap -b I then get typescript errors in the build pipeline
  • bit snap" then bit buildI now get thetypescript error: "message": "Debug Failure." but with no context on what failed.

@sbland
Copy link
Author

sbland commented May 20, 2024

After some more testing with a stripped back workspace I found that everything worked until I added one of my react env components that was referencing a node-env component in the types.

I.e.

import { apiEndpoint } from 'my-node-env-component`;

export const apiCall = (): ReturnType<typeof apiEndpoint> => {
...
}

Defining the type explicitly mostly fixed the problems I was having. I'm not sure why this resulted in random type error in my node-env files.

For now I think I have fixed this for my environment but I'd be happy to help if anyone has a similar issue.

@sbland
Copy link
Author

sbland commented May 23, 2024

This issue cropped up again and I'm now certain it is having a component in an environment with a tsconfig with strict: true referencing a component with a tsconfig with strict: false. In this case check types and the IDE do not pick up on strict typing but the builder does. @GiladShoham what should happen here? Should check-types and the builder be consistent and both fail or should neither fail?

@GiladShoham
Copy link
Member

Any chance you can share your workspace?
@davidfirst fyi

@sbland
Copy link
Author

sbland commented Jun 14, 2024

I've added a example here: https://github.com/sbland/bit-issue-8889
Run bit install bit lint && bit check-types this passes. Then bit snap -m "tmp" -b this fails.

@sbland
Copy link
Author

sbland commented Jun 14, 2024

stdout:

⚠ env: my.scope/envs/react-env-custom@ea5c27c8da1c1ab74d459be414ebfa8bca1facd7, task "teambit.preview/preview:PreBundlePreview" has skipped due to "teambit.compilation/compiler:TypescriptCompile" failure
✖ Running build pipeline using 3 environment(s), total 33 tasks. Failed in 5s

The following errors were found while running the build pipeline
Failed task 1: "teambit.compilation/compiler:TypescriptCompile" of env "my.scope/envs/react-env-custom@ea5c27c8da1c1ab74d459be414ebfa8bca1facd7"
component: my.scope/demos/demo-card@24cb4bb4ab3fba28286bf703669392ff17fb98c6
.../node_modules/.pnpm/file+my.scope_core_lib@37eb5fdd93a2cfd748e4a94373768e091768d16f_@[email protected][email protected]/node_modules/@my/scope.core.lib/lib.tsx:1:21 - error TS7006: Parameter 'something' implicitly has an 'any' type.

1 export const Foo = (something) => something || 'bar';
                      ~~~~~~~~~


Found 1 errors in 1 components


✖ Total 33 tasks. 6 succeeded. 1 failed. 26 skipped. Total errors: 1.
(base) my-directory$ bit check-types
completed type checking (8.858 sec). no errors were found.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants