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

@nx/js:tsc - wrong extends path used when generated temporary tsconfig #28316

Open
1 of 4 tasks
SebastienGllmt opened this issue Oct 6, 2024 · 0 comments · May be fixed by #28317
Open
1 of 4 tasks

@nx/js:tsc - wrong extends path used when generated temporary tsconfig #28316

SebastienGllmt opened this issue Oct 6, 2024 · 0 comments · May be fixed by #28317

Comments

@SebastienGllmt
Copy link

SebastienGllmt commented Oct 6, 2024

Current Behavior

Background

When building with the tsc executor, a temporary tsconfig file gets generated for you in {workspaceRoot}/tmp/{relativeProjectRoot}

This temporary tsconfig file is meant to use the tsconfig extends syntax to extend the tsconfig in your project root with some custom behavior based on your build settings

ex:

{
  "extends": "../../../../../packages/backend/db/tsconfig.lib.json"
}

The code

Internally, this executor is called here

The first thing it does is take the tsconfig path in your executor's configuration and normalizes it. For example

"build": {
  "executor": "@nx/js:tsc",
  "outputs": [
	"{options.outputPath}"
  ],
  "options": {
	"outputPath": "dist/packages/shared/data-types",
	"main": "packages/shared/data-types/src/index.ts",
	"tsConfig": "packages/shared/data-types/tsconfig.lib.json"
  }
}

will turn the relative tsConfig path into '/home/user/Documents/git/my-project/packages/backend/db/tsconfig.lib.json'

This new absolute path is then passed down to create the temporary tsconfig file in the following steps

  1. This line to call into checkDependencies here
  2. checkDependencies calls createTmpTsConfig here
  3. The exact value for the extends field is calculated in readTsConfigWithRemappedPaths here
  4. And ``readTsConfigWithRemappedPaths` then calculated the exact path here

The bug

Note that for the value of extends to be calculated correctly in readTsConfigWithRemappedPaths, you have two options:

  1. Make both paths (dirnameTsConfig, tsConfig) absolute
  2. Make both paths relative to workspaceRoot

However, instead, right now dirnameTsConfig is turned into a relative path while tsConfig is kept as an absolute path (remember we normalized it here)

This results in the value of generatedTsConfig.extends to be incorrect, causing builds to fail

Expected Behavior

The paths should either both be absolute, or both be relative.

The easiest way to do this was to pass in the relative path into the checkDependencies function (since the tsConfig variable is not accessed anywhere except to calculate generatedTsConfig.extends), but changing dirnameTsConfig to always be absolute would also be okay

GitHub Repo

https://github.com/SebastienGllmt/nx-tsc-repro

Steps to Reproduce

See repro README

Builds seems to work fine for me on my repo.... how come you can reproduce it?

The reason this error wasn't caught before is presumably because if the extends path points to an invalid path, builds will still work if they were going to work with the default tsc configuration.

Notably, if you add console.log(tsConfig) here, you can see that these builds actually result in the following error that gets supressed

errors: [
    {
      file: undefined,
      start: undefined,
      length: undefined,
      messageText: "Cannot read file '/home/user/Documents/git/my-project/tsconfig.lib.json'.",
      category: 1,
      code: 5083,
      reportsUnnecessary: undefined,
      reportsDeprecated: undefined
    },
    {
      file: undefined,
      start: undefined,
      length: undefined,
      messageText: `No inputs were found in config file 'tsconfig.json'. Specified 'include' paths were '["**/*"]' and 'exclude' paths were '[]'.`,
      category: 1,
      code: 18003,
      reportsUnnecessary: undefined,
      reportsDeprecated: undefined
    }
  ],

Reliably causing an actual build failure

Although these builds always have a configuration error, as I mentioned that doesn't always actually lead to a build error

  1. nx build

Nx Report

Node           : 20.11.1
OS             : linux-x64
Native Target  : x86_64-linux
npm            : 10.2.4

nx                 : 19.8.2
@nx/js             : 19.8.2
@nx/linter         : 19.8.2
@nx/eslint         : 19.8.2
@nx/workspace      : 19.8.2
@nx/devkit         : 19.8.2
@nx/eslint-plugin  : 19.8.2
@nrwl/tao          : 19.8.2
@nx/vite           : 19.8.2
@nx/web            : 19.8.2
typescript         : 5.6.2
---------------------------------------
Registered Plugins:
@nx/eslint/plugin

Failure Logs

error TS5090: Non-relative paths are not allowed when 'baseUrl' is not set. Did you forget a leading './'?

Cannot read file '/home/user/Documents/git/my-project/tsconfig.lib.json'

Package Manager Version

pnpm 9.6.0

Operating System

  • macOS
  • Linux
  • Windows
  • Other (Please specify)

Additional Information

No response

@SebastienGllmt SebastienGllmt changed the title Uses the wrong extends path when generated temporary tsconfig @nx/js:tsc - wrong extends path used when generated temporary tsconfig Oct 6, 2024
SebastienGllmt added a commit to SebastienGllmt/nx that referenced this issue Oct 6, 2024
@SebastienGllmt SebastienGllmt linked a pull request Oct 6, 2024 that will close this issue
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants