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: hostOrText.readFile is not a function when extending @tsconfig/node18 #14223

Closed
avidj opened this issue Jan 9, 2023 · 4 comments · Fixed by #14380
Closed

TypeError: hostOrText.readFile is not a function when extending @tsconfig/node18 #14223

avidj opened this issue Jan 9, 2023 · 4 comments · Fixed by #14380

Comments

@avidj
Copy link

avidj commented Jan 9, 2023

Current Behavior

Given the devDependency "@tsconfig/node18": "1.0.1"
and a tsconfig.json with "extends": "@tsconfig/node18"
every target (here: linting) results in

TypeError: hostOrText.readFile is not a function
Occurred while linting .../quality/large-test-base/src/assertions.ts:1
Rule: "@nrwl/nx/enforce-module-boundaries"
    at readJsonOrUndefined (.../node_modules/typescript/lib/typescript.js:19519:74)
    at Object.readJson (.../node_modules/typescript/lib/typescript.js:19528:16)

The reason is a call to readFile in typescript/typescript.js line 19519:
var jsonText = ts.isString(hostOrText) ? hostOrText : hostOrText.readFile(path);

It can be interpreted as a breaking change in typescript where a surrounding try-block was removed:
microsoft/TypeScript#51644

However, IMHO at least judging from the referenced discussion, it also seems to be a bug in nx as it provides a host that is missing the readFile method in nx/utils/typescript.js line 23:

const host = {
        readDirectory: () => [],
        fileExists: tsModule.sys.fileExists,
    };

Remark: I got the stack trace with linting. But there were also situations (I think just tsc) where I only got the error message. This made tracking down the problem unnecessarily difficult.

Expected Behavior

no error

Github Repo

No response

Steps to Reproduce

See above

  1. add dependency "@tsconfig/node18": "1.0.1"`
  2. add "extends": "@tsconfig/node18" to tsconfig.json
  3. npx nx lint

Nx Report

[547][d068511.C02C78CRMD6T: sapgraph]$ npx nx report

 >  NX   hostOrText.readFile is not a function

   
   Because of the error the Nx daemon process has exited. The next Nx command is going to restart the daemon process.
   If the error persists, please run "nx reset".

[548][d068511.C02C78CRMD6T: sapgraph]$

Failure Logs

Occurred while linting /project-root/quality/large-test-base/src/assertions.ts:1
   Rule: "@nrwl/nx/enforce-module-boundaries"

TypeError: hostOrText.readFile is not a function
Occurred while linting /project-root/quality/large-test-base/src/assertions.ts:1
Rule: "@nrwl/nx/enforce-module-boundaries"
    at readJsonOrUndefined (/project-root/node_modules/typescript/lib/typescript.js:19519:74)
    at Object.readJson (/project-root/node_modules/typescript/lib/typescript.js:19528:16)
    at getPackageJsonInfo (/project-root/node_modules/typescript/lib/typescript.js:44767:41)
    at loadModuleFromSpecificNodeModulesDirectory (/project-root/node_modules/typescript/lib/typescript.js:45316:27)
    at loadModuleFromImmediateNodeModulesDirectory (/project-root/node_modules/typescript/lib/typescript.js:45296:58)
    at /project-root/node_modules/typescript/lib/typescript.js:45286:39
    at Object.forEachAncestorDirectory (/project-root/node_modules/typescript/lib/typescript.js:8224:26)
    at loadModuleFromNearestNodeModulesDirectoryWorker (/project-root/node_modules/typescript/lib/typescript.js:45280:19)
    at loadModuleFromNearestNodeModulesDirectory (/project-root/node_modules/typescript/lib/typescript.js:45272:16)
    at tryResolve (/project-root/node_modules/typescript/lib/typescript.js:44358:34)

Additional Information

The attached patch fixes the problem. As a workaround, you may want to apply it with patch-package as postinstall.
nx+15.4.4.patch

@avidj avidj added the type: bug label Jan 9, 2023
@AgentEnder
Copy link
Member

I see you have a patch which fixes the issue. Would you like to open a PR?

@AgentEnder AgentEnder self-assigned this Jan 9, 2023
@aukevanleeuwen
Copy link
Contributor

Same issue here as well, upgraded to TypeScript 4.9 and started failing. We're under a bit of pressure so didn't have time to look into it deeply, but it's the same situation (be it with @tsconfig/node16).

Can confirm that patching ./node_modules/nx/src/utils/typescript.js works.

Note

I first patched ./node_modules/@nrwl/workspace/src/utilities/typescript.js, which had a very similar

function readTsConfigOptions(tsConfigPath) {
  // ...
}

But that didn't seem to do anything for my use case. Just wanted to say that it's in the code base twice:

  1. // we don't need to scan the files, we only care about options
    const host = {
    readDirectory: () => [],
    fileExists: tsModule.sys.fileExists,
    };
  2. // we don't need to scan the files, we only care about options
    const host: Partial<ts.ParseConfigHost> = {
    readDirectory: () => [],
    fileExists: tsModule.sys.fileExists,
    };

@aukevanleeuwen
Copy link
Contributor

@AgentEnder: I've created a PR for it: #14380

@github-actions
Copy link

github-actions bot commented Mar 3, 2023

This issue has been closed for more than 30 days. If this issue is still occuring, please open a new issue with more recent context.

@github-actions github-actions bot locked as resolved and limited conversation to collaborators Mar 3, 2023
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.

3 participants