Skip to content

[BREAKING] make --script-mode the default; add --cwd-mode to switch back to old behavior - #1155

Merged
cspotcode merged 24 commits into
masterfrom
ab/script-mode-by-default
Feb 16, 2021
Merged

cspotcode merged 24 commits into
masterfrom
ab/script-mode-by-default

Conversation

@cspotcode

@cspotcode cspotcode commented Nov 19, 2020 •

Copy link
Copy Markdown
Collaborator

Resolves #949
Resolves #1194
Resolves #1189
Resolves #1197
Resolves #1220
Resolves #1225

  • add tests

I took a quick look at our old discussions and it doesn't seem like we came up with any reasons why this change is bad.

Adds --cwd-mode flag to switch back to the old behavior, or users can specify a directory with --dir or a tsconfig file with --project

Need to rethink the way --dir works. Currently, it is treated as if you'd cded to the target directory, which affects lots of behaviors. The --help docs say it only affects config resolution. But in the code, it overrides all process.cwd() checks. That seems confusing to me.

For example, if you use --dir, then error messages which log script paths will log them relative to --dir. --project is also interpreted relative to --dir.


Now that implementation is complete, a summary of changes:

  • Use --script-mode by default
  • Add --cwd-mode flag to opt-out of --script-mode
  • Add ts-node-cwd entry-point that uses --cwd-mode by default
  • Rename --dir to --cwd; TS_NODE_DIR to TS_NODE_CWD; parse legacy names for backwards compatibility
  • Rewrite --cwd docs to say it changes effective cwd
  • Add projectSearchDir API option to set directory from which tsconfig search is performed
  • Fix bug where resolving entry-point location could poison require.resolve cache and prevent correct extension from loading (ts-node ./index may execute the wrong file extension because tsconfig search poisons the require.resolve cache #1220)
  • --cwd no longer sets --scope
  • Remove --scope from CLI flags and tsconfig-loaded options; it is an API-only option, to match the intended use-case: programmatically installing multiple ts-node instances
  • Add scopeDir API option
  • Deprecate TS_NODE_SCOPE env var
  • ignore rules evaluated relative to tsconfig.json, otherwise cwd; no longer tied to --dir
  • compiler is loaded relative to tsconfig.json instead of cwd or entrypoint script (Load compiler relative to tsconfig.json instead of cwd #1225)

@codecov

codecov Bot commented Nov 19, 2020 •

Copy link
Copy Markdown

Codecov Report

Merging #1155 (1811706) into master (080af32) will increase coverage by 0.38%.
The diff coverage is 84.78%.

Impacted Files Coverage Δ
src/bin.ts 90.10% <75.00%> (-0.14%) ⬇️
src/index.ts 79.75% <91.66%> (+0.44%) ⬆️
src/bin-cwd.ts 100.00% <100.00%> (ø)

@cspotcode
cspotcode merged commit bacbeaf into master Feb 16, 2021
@cspotcode cspotcode changed the title make --script-mode the default; add --cwd-mode to switch back to old behavior [BREAKING] make --script-mode the default; add --cwd-mode to switch back to old behavior Feb 22, 2021
@cspotcode cspotcode added this to the 10.0.0 milestone May 19, 2021
@cspotcode
cspotcode deleted the ab/script-mode-by-default branch October 11, 2021 03:48
hanzo-dev pushed a commit to hanzoai/ts-node that referenced this pull request Sep 9, 2026
…behavior; other cwd, project, and dir fixes (TypeStrong#1155)

* Use `--script-mode` by default
* Add `--cwd-mode` flag to opt-out of `--script-mode`
* Add `ts-node-cwd` entry-point that uses `--cwd-mode` by default
* Rename `--dir` to `--cwd`; `TS_NODE_DIR` to `TS_NODE_CWD`; parse legacy names for backwards compatibility
* Rewrite `--cwd` docs to say it changes effective `cwd`
* Add `projectSearchDir` API option to set directory from which tsconfig search is performed
* Fix bug where resolving entry-point location could poison `require.resolve` cache and prevent correct extension from loading (TypeStrong#1220)
* `--cwd` no longer sets `--scope`
* Remove `--scope` from CLI flags and tsconfig-loaded options; it is an API-only option, to match the intended use-case: programmatically installing multiple ts-node instances
* Add `scopeDir` API option
* Deprecate `TS_NODE_SCOPE` env var
* `ignore` rules evaluated relative to `tsconfig.json`, otherwise `cwd`; no longer tied to `--dir`
* `compiler` is loaded relative to `tsconfig.json` instead of `cwd` or entrypoint script (TypeStrong#1225)

---

*Original GH-generated squash summary*

* make --script-mode the default; add --cwd-mode to switch back to old behavior

* Fix bug where --script-mode entrypoint require.resolve poisons the require.resolve cache; causes entrypoint to resolve incorrectly when --prefer-ts-exts is used

* WIP TODO amend / rewrite this commit

* wip

* WIP

* add ts-node-cwd bin, which is equivalent to ts-node --cwd-mode

* rename projectSearchPath to projectSearchDir

* Revert undesirable changes from WIP commits

* add --cwd-mode and --script-mode tests

* revert undesirable logging from WIP commits

* update tests which relied on --dir affecting to cwd to instead use projectSearchDir as needed

* remove --script-mode from test invocations that don't need it anymore

* fix lint failures

* fix tests

* fix requireResolveNonCached to avoid hack on node 10 & 11

* fix tests to avoid type error on ts2.7

* fix tests on node 10

* update README and final cleanup

* more cleanup

* Load typescript compiler relative to tsconfig.json
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

1 participant