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
{{ message }}
Repository navigation
Changes to --dir, --cwd, --script-mode, add configSearchPath #1197
@blakeembrey Do you have time to offer a sanity-check on these breaking changes? You might have some helpful historical perspective that I don't. I suspect that ts-node's current behavior is an understandable result of organic evolution over time, but we can make some changes to keep it clean and intuitive.
While working on #1155 I realized that --dir and --script-mode have some interesting behaviors. --dir is documented to change the location of config file resolution, but it actually does much more: it overrides process.cwd(). As far as I know, this affects TS_NODE_SCOPE and --project in a confusing way. For example, ts-node-script ./src/bin/main.ts will set cwd to ./src/bin, affecting the interpretation of --scope and --project, and affecting relative paths logged in error messages. A user would be confused by that.
With that in mind, do the following changes sound reasonable?
Proposed Changes
rename --dir to --cwd so behavior is more intuitive. Keep --dir as deprecated for backwards compat.
Rewrite --cwd docs to state that it overrides the cwd, which affects lots of things (can copy phrasing from git -C docs)
[BREAKING] update --script-mode so that it affects tsconfig discovery but not cwd. Can accomplish via a new configSearchPath API option.
Questions
Is configSearchPath a good name? configSearchDir?
Should configSearchPath be exposed as a CLI flag / env var? I say no
Pretty sure this is just my fault and no explicit reasoning, seemed logical at the time to have them affect everything because it's acting as if you were in that directory. IIRC the config flag was originally called --cwd but I renamed it at some point. I'm not sure how used --scope is in the real world, it was a workaround to allow multiple ts-node instances to be registered (e.g. allows ts-node to be registered from a node module which we've historically disallowed as a global automatic behavior).
I think the error paths might have been a mistake, didn't think through that one. I suspect it should have always used process.cwd() in that case, separate from the --dir flag. For --dir and --scope, I'd just want to make sure we can keep the linked package use-case alive (allow multiple ts-node instances to be registered for different paths). In that case, it makes a little less sense to think of it as a --cwd flag.
With the proposed change, would it help to write out how these things affect each other? I think when it comes to --dir, --scope and --script-mode there's some odd combos I never expect to see - specifically --scope and --script-mode. When you mention "so that it affects tsconfig discovery but not cwd" I'm assuming you're talking about how much --dir affects today? I agree with that, that'd be a first step that de-couples --dir and --script-mode.
Question: do you think --script-mode should affect --dir as it relates to --scope? Is there any other --dir behaviors we're missing?
Is configSearchPath a good name?
SGTM.
Should configSearchPath be exposed as a CLI flag / env var? I say no
Also seems reasonable to start with it internal only.
Ok, thanks, good idea to enumerate all sensible and non-sensible combinations of flags. Also, it's possible I've subtly broken --dir's behavior over time, as I changed surrounding code.
Question: do you think --script-mode should affect --dir as it relates to --scope? Is there any other --dir behaviors we're missing?
It seems most useful for --dir to default to either a) directory containing tsconfig.json or b) rootDir according to tsconfig.json
When you mention "so that it affects tsconfig discovery but not cwd" I'm assuming you're talking about how much --dir affects today?
Now I'm thinking --dir's only effect should be setting the directory for --scope. Everything else can be driven by --project, projectSearchPath, --script-mode, and (rarely) --cwd.
Use-cases
categorized as either useful to CLI consumers, API consumers, or both
CLI: from shebang, find tsconfig.json corresponding to entry-point file: --script-mode
changed the title [-]Changes to --dir, --cwd, --script-mode, add `configSearchPath`[/-][+][BREAKING] Changes to --dir, --cwd, --script-mode, add `configSearchPath`[/+]on Feb 22, 2021
changed the title [-][BREAKING] Changes to --dir, --cwd, --script-mode, add `configSearchPath`[/-][+]Changes to --dir, --cwd, --script-mode, add `configSearchPath`[/+]on Feb 22, 2021
Activity
[-]Changes to --dir, --cwd, --script-mode[/-][+]Changes to --dir, --cwd, --script-mode, add `configSearchPath`[/+]blakeembrey commentedon Jan 19, 2021
Pretty sure this is just my fault and no explicit reasoning, seemed logical at the time to have them affect everything because it's acting as if you were in that directory. IIRC the config flag was originally called
--cwdbut I renamed it at some point. I'm not sure how used--scopeis in the real world, it was a workaround to allow multiplets-nodeinstances to be registered (e.g. allowsts-nodeto be registered from a node module which we've historically disallowed as a global automatic behavior).I think the error paths might have been a mistake, didn't think through that one. I suspect it should have always used
process.cwd()in that case, separate from the--dirflag. For--dirand--scope, I'd just want to make sure we can keep the linked package use-case alive (allow multiplets-nodeinstances to be registered for different paths). In that case, it makes a little less sense to think of it as a--cwdflag.With the proposed change, would it help to write out how these things affect each other? I think when it comes to
--dir,--scopeand--script-modethere's some odd combos I never expect to see - specifically--scopeand--script-mode. When you mention "so that it affects tsconfig discovery but not cwd" I'm assuming you're talking about how much--diraffects today? I agree with that, that'd be a first step that de-couples--dirand--script-mode.Question: do you think
--script-modeshould affect--diras it relates to--scope? Is there any other--dirbehaviors we're missing?SGTM.
Also seems reasonable to start with it internal only.
cspotcode commentedon Jan 19, 2021
Ok, thanks, good idea to enumerate all sensible and non-sensible combinations of flags. Also, it's possible I've subtly broken
--dir's behavior over time, as I changed surrounding code.It seems most useful for
--dirto default to either a) directory containingtsconfig.jsonor b)rootDiraccording totsconfig.jsonNow I'm thinking
--dir's only effect should be setting the directory for--scope. Everything else can be driven by--project,projectSearchPath,--script-mode, and (rarely)--cwd.Use-cases
categorized as either useful to CLI consumers, API consumers, or both
--script-mode--project/project--scope/scopeand--scopeDir/scopeDirprojectSearchPathLess common use-cases
--cwd/cwdPermutations
The messiest conceivable combinations, with all flags being optional:
--cwd,--scope,--scopeDir, and--script-mode(and via API)--cwd,--scope,--scopeDir, and--cwd-mode(if added by [BREAKING] make --script-mode the default; add --cwd-mode to switch back to old behavior #1155) (and via API)--cwd,--scope,--scopeDir, and--project(and via API)cwd,scope,scopeDir, andprojectSearchPath(only via API, not CLI)In all cases,
--scopeDirdefaults to the directory containing the tsconfig file.Questions
--dirbe renamed to--scopeDir?--scopeDirdefault to the directory containing the config file? Or should it default torootDir, which is more complex, but maybe more correct?projectSearchPathsound? It'll match theprojectoption.scopeandscopeDirinto a single option?[-]Changes to --dir, --cwd, --script-mode, add `configSearchPath`[/-][+][BREAKING] Changes to --dir, --cwd, --script-mode, add `configSearchPath`[/+][-][BREAKING] Changes to --dir, --cwd, --script-mode, add `configSearchPath`[/-][+]Changes to --dir, --cwd, --script-mode, add `configSearchPath`[/+]