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

ts-node REPL treats unparenthesized object literals as block scopes, doesnt align with node REPL behavior #1697

Closed
jhmaster2000 opened this issue Mar 24, 2022 · 8 comments · Fixed by #1699
Labels
you can do this Good candidate for a pull request.
Milestone

Comments

@jhmaster2000
Copy link
Contributor

Search Terms

repl object literal block scope

Expected Behavior

Input string: { key: "value" }
Node REPL behavior, also expected from ts-node:
SmartSelect_20220324-030402_Termux

Actual Behavior

Same input string, how ts-node REPL behaves:
SmartSelect_20220324-030606_Termux

Steps to reproduce the problem

In a terminal with ts-node installed:

  1. Run ts-node
  2. Enter { key: "value" }
  3. Notice how {}'s were treated as a block scope, key as a (unused) label, leaving "value" as the resulting string literal of the expression, instead of the expected object as seen in node REPL above

Minimal reproduction

N/A (See above)

Specifications

  • ts-node version: v10.7.10
  • node version: v16.14.0
  • TypeScript version: v4.6.2
  • tsconfig.json, if you're using one: N/A (REPL)
  • Operating system and version: Android 11 aarch64 (Termux 0.118.0)
@cspotcode
Copy link
Collaborator

To debug this you'll have to check where we're transforming REPL inputs from TS to JS and inspect the output. You can see in repl.ts or repl.js where we call the compiler, then use a diffing library to identify the parts that came from your most recent input. Take a look at the output from the TS compiler.

@cspotcode cspotcode added the you can do this Good candidate for a pull request. label Mar 24, 2022
@jhmaster2000
Copy link
Contributor Author

jhmaster2000 commented Mar 28, 2022

@cspotcode hey there, I am taking a stab at this, however I have encountered a roadblock with tests, they seem to be entirely broken on ts-node's current master branch? Is this correct or am I doing something wrong somehow?
image

The above is the result of literally just git clone https://github.com/TypeStrong/ts-node.git, then running npm install followed by npm run test-local, a fresh clean copy of the master branch without any of my changes, just to make sure it wasn't something I wrote in the code breaking it indirectly somehow... My only thought is maybe tests only work on a specific Node version? But if so, which one, and is it documented anywhere...?

It errors on this line, which doesn't really make much sense to me:

expect(() => {createRequire(resolve(__dirname, 'tests/foo.js')).resolve('ts-node')}).toThrow();

@jhmaster2000
Copy link
Contributor Author

Update
I have found the cause of the issue above, it's caused by having NODE_PATH environment variable set, containing a path where ts-node is installed. The fix is simple, to just blank out NODE_PATH before running ava, however this adds a new dev dependency, cross-env, is this acceptable? Not sure on the policies on adding new dependencies for this repository.

@cspotcode
Copy link
Collaborator

cspotcode commented Mar 28, 2022

Our tests run nightly on a range of node versions and typescript versions, both Linux and Windows. Check GitHub Actions to see if they are passing and to see the test configuration. Read CONTRIBUTING.md for more information.

You should be able to manipulate environment variables in our ava config file: ava.config.cjs

@jhmaster2000
Copy link
Contributor Author

I did notice the environmentVariables option in ava.config.cjs, unfortunately that doesn't work, as by the time those are applied, node already parsed NODE_PATH and cached it internally, so changing it at runtime is useless, hence why I had to blank it out prior to running ava using cross-env.

@cspotcode
Copy link
Collaborator

I'm hesitant to add this bit of complexity since, as far as I know, no one else has hit this problem. You can choose to unset NODE_PATH in whatever way is most convenient for you to write tests. And if/when you submit a pull request, it will run correctly on Github Actions because Github Actions does not set that environment variable either.

However, we can add a warning to ava.config.cjs that detects if NODE_PATH has been set and instructs the developer to unset it prior to any development work. The warning can include a link to this thread, too.

@jhmaster2000
Copy link
Contributor Author

There isn't really any added complexity on the user-side if cross-env is simply added to the test-local script, you will still just run npm run test-local as normal, and you can even still run ava directly if you don't have NODE_PATH set, it will also work as usual, it's pretty much what you suggest, but it automatically corrects it for tests rather than asking the user to unset it on their end, which can get annoying fast, myself for example, I have other projects on my machine which rely on NODE_PATH to work correctly, so just ditching it isn't viable, and remembering to unset it everytime to run tests will get bothersome rather than just have it nicely automatically clear the env var. I do however understand and am aware I appear to be an edge case indeed, so I can see where you are coming from with not wanting to add an extra dev dependency just because 1 person had an issue, but I wanted to further clarify how there's no real added complexity to the development process as I felt like that may have been unclear a bit? If you really don't want to do it I understand and can do your suggestion of a warning instead, it is what I had originally gone for before realizing it was fixable by clearing the variable anyway.

@cspotcode
Copy link
Collaborator

That's a fair point, but I'd rather go with the warning. If it's easier for you, feel free to add cross-env to your pull request, and I'll remove it before merging. I don't mind doing that.

jhmaster2000 added a commit to jhmaster2000/ts-node that referenced this issue Mar 29, 2022
@cspotcode cspotcode added this to the 10.8.0 milestone Mar 30, 2022
cspotcode added a commit that referenced this issue Apr 20, 2022
* Implement unparenthesized REPL object literals

* Fix property access error inconsistency

* Run prettier on src/repl.ts

* Add cross-env as dev dependency

* Fix issue preventing tests from running on envs with NODE_PATH set

* Silence deprecation warning spam on tests

* Single quotes caused some cli bugs

* Add REPL object literal tests

* remove cross-env

* Add NODE_PATH check and warning to tests runner

See: #1697 (comment)

* Run prettier on repl.spec.ts

* node nightly tests fail because of unexpected custom ESM loaders warnings so fix that i guess?

* fix tests on TS 2.7

* node nightly tests still failed, fix attempt 2

* append instead of overriding NODE_OPTIONS

* accept warning-only stderr on nightly test

* if check didnt fire

* maybe the regex is broken

* am i even editing the right lines

* make test-cov match test-spec

* try checking for nightly again...

* tests work! clean them up now, please don't break

* Remove node nightly tests warning checks
(superseded by #1701)

* simplify NODE_PATH check

* attempt at running new repl tests in-process for speed

* switch tests to run in-process using existing macros

* finish changes  to run tests in-process

Co-authored-by: Andrew Bradley <cspotcode@gmail.com>
crapStone pushed a commit to Calciumdibromid/CaBr2 that referenced this issue May 30, 2022
This PR contains the following updates:

| Package | Type | Update | Change |
|---|---|---|---|
| [ts-node](https://typestrong.org/ts-node) ([source](https://github.com/TypeStrong/ts-node)) | devDependencies | minor | [`10.7.0` -> `10.8.0`](https://renovatebot.com/diffs/npm/ts-node/10.7.0/10.8.0) |

---

### Release Notes

<details>
<summary>TypeStrong/ts-node</summary>

### [`v10.8.0`](https://github.com/TypeStrong/ts-node/releases/tag/v10.8.0)

[Compare Source](TypeStrong/ts-node@v10.7.0...v10.8.0)

Questions about this release? Ask in the official discussion thread: [#&#8203;1767](TypeStrong/ts-node#1767)

**Added**

-   Added support for `module=NodeNext`, `module=Node16`, `.mts`, `.cts`, `.mjs`, and `.cjs` file extensions ([#&#8203;1414](TypeStrong/ts-node#1414), [#&#8203;1694](TypeStrong/ts-node#1694), [#&#8203;1744](TypeStrong/ts-node#1744), [#&#8203;1745](TypeStrong/ts-node#1745), [#&#8203;1727](TypeStrong/ts-node#1727), [#&#8203;1717](TypeStrong/ts-node#1717), [#&#8203;1753](TypeStrong/ts-node#1753), [#&#8203;1757](TypeStrong/ts-node#1757)) [@&#8203;cspotcode](https://github.com/cspotcode)
    -   For best results, enable `experimentalResolver` ([docs](https://typestrong.org/ts-node/docs/options#experimentalresolver))
    -   See TypeScript's official documentation: https://www.typescriptlang.org/docs/handbook/esm-node.html
    -   enables mixed-mode projects with both ESM and CommonJS
    -   enables all supported file extensions in TypeScript 4.7
    -   Obeys package.json "type"
-   Added ability to include file extensions in CommonJS imports ([#&#8203;1727](TypeStrong/ts-node#1727), [#&#8203;1753](TypeStrong/ts-node#1753)) [@&#8203;cspotcode](https://github.com/cspotcode)
    -   Enables consistency with ESM, where file extensions are often mandatory
-   Resolves from emitted to source file extensions ([#&#8203;1727](TypeStrong/ts-node#1727), [#&#8203;1753](TypeStrong/ts-node#1753)) [@&#8203;cspotcode](https://github.com/cspotcode)
    -   Must enable `experimentalResolver`, will be enabled by default in a future version ([docs](https://typestrong.org/ts-node/docs/options#experimentalresolver))
    -   Typechecker requires importing the *emitted* file extension; ts-node resolves correctly to the *source* file.  E.g. `import "./foo.js"` will execute `foo.ts` See also: [TypeScript issue #&#8203;37582](microsoft/TypeScript#37582)
    -   If typechecking is disabled, you can also use *source* file extensions.  E.g. `import "./foo.ts"`
-   Added `experimentalSpecifierResolution` ([#&#8203;1727](TypeStrong/ts-node#1727), [#&#8203;1753](TypeStrong/ts-node#1753)) [@&#8203;cspotcode](https://github.com/cspotcode)
    -   the same as Node's `--experimental-specifier-resolution` ([Node docs](https://nodejs.org/dist/latest-v18.x/docs/api/esm.html#customizing-esm-specifier-resolution-algorithm))
    -   can also be specified in `tsconfig.json` for convenience, to avoid the CLI flag
    -   allows omitting file extensions in ESM imports, plus a few other CommonJS-style conveniences
-   Adds `diagnostics` property to `TSError`, with array of TypeScript diagnostic objects from the compiler ([API docs](https://typestrong.org/ts-node/api/classes/TSError.html)) ([#&#8203;1705](TypeStrong/ts-node#1705), [#&#8203;1706](TypeStrong/ts-node#1706)) [@&#8203;paulbrimicombe](https://github.com/paulbrimicombe)

**Changed**

-   Renames option `experimentalResolverFeatures` to `experimentalResolver` ([docs](https://typestrong.org/ts-node/docs/options#experimentalresolver)) ([#&#8203;1727](TypeStrong/ts-node#1727)) [@&#8203;cspotcode](https://github.com/cspotcode)
-   Internal change to ESM loader for compatibility with forthcoming node versions: returns `shortCircuit: true` ([#&#8203;1714](TypeStrong/ts-node#1714), [#&#8203;1715](TypeStrong/ts-node#1715)) [@&#8203;cspotcode](https://github.com/cspotcode)
-   Performance: Optimize filesystem stat calls in ESM loader and new CommonJS resolver ([#&#8203;1758](TypeStrong/ts-node#1758), [#&#8203;1759](TypeStrong/ts-node#1759)) [@&#8203;cspotcode](https://github.com/cspotcode)
-   Performance, maintenance: Upgrade source-mapper dependency "[@&#8203;cspotcode/source-map-support](https://github.com/cspotcode/source-map-support)"
    -   Switches to "trace-mapping" for underlying source-map parsing ([#&#8203;1729](TypeStrong/ts-node#1729)) [@&#8203;cspotcode](https://github.com/cspotcode)

**Fixed**

-   Fixed bug where REPL `.type` command was not showing any type information when using TypeScript nightly builds ([#&#8203;1761](TypeStrong/ts-node#1761), [#&#8203;1762](TypeStrong/ts-node#1762)) [@&#8203;cspotcode](https://github.com/cspotcode)
-   Correctly suppress "Custom ESM Loaders" warning on newer node versions where the warning's prose changed ([#&#8203;1701](TypeStrong/ts-node#1701)) [@&#8203;cspotcode](https://github.com/cspotcode)
-   Fixed REPL bug where function signatures could not be entered across multiple lines ([#&#8203;1667](TypeStrong/ts-node#1667), [#&#8203;1677](TypeStrong/ts-node#1677)) [@&#8203;d9k](https://github.com/d9k)
-   REPL treats unparenthesized object literals as objects, instead of as block scopes ([#&#8203;1697](TypeStrong/ts-node#1697), [#&#8203;1699](TypeStrong/ts-node#1699)) [@&#8203;jhmaster2000](https://github.com/jhmaster2000)
-   Fixed bug where `preferTsExts` combined with third-party transpiler hooks could disrupt `nyc` code coverage ([#&#8203;1755](TypeStrong/ts-node#1755)) [@&#8203;cspotcode](https://github.com/cspotcode)
-   Fixed bug where `file://` URLs in stack traces did not always use percent-encoding ([#&#8203;1738](TypeStrong/ts-node#1738), [#&#8203;1726](TypeStrong/ts-node#1726), [#&#8203;1729](TypeStrong/ts-node#1729)) [@&#8203;cspotcode](https://github.com/cspotcode)
-   Fixed bug where v8-compile-cache-lib did not correctly unhook itself ([#&#8203;1717](TypeStrong/ts-node#1717), [#&#8203;1718](TypeStrong/ts-node#1718), [#&#8203;1719](TypeStrong/ts-node#1719)) [@&#8203;cspotcode](https://github.com/cspotcode)
    -   This internal dependency is used to speed up loading the TypeScript compiler

**Docs**

-   Many docs improvements ([#&#8203;1682](TypeStrong/ts-node#1682)) [@&#8203;cspotcode](https://github.com/cspotcode)
-   Options page: each option its own linkable header w/usage example ([#&#8203;1606](TypeStrong/ts-node#1606)) [@&#8203;cspotcode](https://github.com/cspotcode)
-   Categorize APIs in typedoc, make entrypoints more prominent ([#&#8203;1456](TypeStrong/ts-node#1456)) [@&#8203;cspotcode](https://github.com/cspotcode)
-   Clarify that the shorthand for `--project` is `-P`, not `-p` ([#&#8203;1731](TypeStrong/ts-node#1731), [#&#8203;1734](TypeStrong/ts-node#1734)) [@&#8203;lobsterkatie](https://github.com/lobsterkatie)
-   Add common ESM errors to Troubleshooting page ([#&#8203;1607](TypeStrong/ts-node#1607)) [@&#8203;cspotcode](https://github.com/cspotcode)

https://github.com/TypeStrong/ts-node/milestone/12

</details>

---

### Configuration

📅 **Schedule**: At any time (no schedule defined).

🚦 **Automerge**: Disabled by config. Please merge this manually once you are satisfied.

♻ **Rebasing**: Whenever PR becomes conflicted, or you tick the rebase/retry checkbox.

🔕 **Ignore**: Close this PR and you won't be reminded about this update again.

---

 - [ ] <!-- rebase-check -->If you want to rebase/retry this PR, click this checkbox.

---

This PR has been generated by [Renovate Bot](https://github.com/renovatebot/renovate).

Co-authored-by: cabr2-bot <cabr2.help@gmail.com>
Reviewed-on: https://codeberg.org/Calciumdibromid/CaBr2/pulls/1367
Reviewed-by: crapStone <crapstone@noreply.codeberg.org>
Co-authored-by: Calciumdibromid Bot <cabr2_bot@noreply.codeberg.org>
Co-committed-by: Calciumdibromid Bot <cabr2_bot@noreply.codeberg.org>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
you can do this Good candidate for a pull request.
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants