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

Fix bug where REPL inputs were compiled as ESM in ESM projects #1959

Merged
merged 4 commits into from
Feb 14, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Next Next commit
change repl virtual filename to .cts extension so it compiles as cjs …
…even in ESM packages with module:nodenext
  • Loading branch information
cspotcode committed Feb 13, 2023
commit d98d95943094487b9ca08a122d93c21272d52b23
2 changes: 1 addition & 1 deletion src/repl.ts
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ export const STDIN_FILENAME = `[stdin].ts`;
/** @internal */
export const STDIN_NAME = `[stdin]`;
/** @internal */
export const REPL_FILENAME = '<repl>.ts';
export const REPL_FILENAME = '<repl>.cts';
/** @internal */
export const REPL_NAME = '<repl>';

Expand Down
8 changes: 4 additions & 4 deletions src/test/repl/repl-environment.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -197,7 +197,7 @@ test.suite(
exportsTest: true,
// Note: vanilla node uses different name. See #1360
stackTest: expect.stringContaining(
` at ${join(TEST_DIR, '<repl>.ts')}:4:`
` at ${join(TEST_DIR, '<repl>.cts')}:4:`
),
moduleAccessorsTest: true,
argv: [tsNodeExe],
Expand Down Expand Up @@ -350,7 +350,7 @@ test.suite(
exportsTest: true,
// Note: vanilla node uses different name. See #1360
stackTest: expect.stringContaining(
` at ${join(TEST_DIR, '<repl>.ts')}:4:`
` at ${join(TEST_DIR, '<repl>.cts')}:4:`
),
moduleAccessorsTest: true,
argv: [tsNodeExe],
Expand Down Expand Up @@ -428,7 +428,7 @@ test.suite(

// Note: vanilla node uses different name. See #1360
stackTest: expect.stringContaining(
` at ${join(TEST_DIR, '<repl>.ts')}:1:`
` at ${join(TEST_DIR, '<repl>.cts')}:1:`
),
},
});
Expand Down Expand Up @@ -459,7 +459,7 @@ test.suite(
exportsTest: true,
// Note: vanilla node uses different name. See #1360
stackTest: expect.stringContaining(
` at ${join(TEST_DIR, '<repl>.ts')}:1:`
` at ${join(TEST_DIR, '<repl>.cts')}:1:`
),
moduleAccessorsTest: true,
},
Expand Down
24 changes: 22 additions & 2 deletions src/test/repl/repl.spec.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,10 @@
import { context, expect } from '../testlib';
import { delay, resetNodeEnvironment, ts } from '../helpers';
import {
CMD_TS_NODE_WITHOUT_PROJECT_FLAG,
delay,
resetNodeEnvironment,
ts,
} from '../helpers';
import semver = require('semver');
import { CMD_TS_NODE_WITH_PROJECT_FLAG, ctxTsNode, TEST_DIR } from '../helpers';
import { createExec, createExecTester } from '../exec-helpers';
Expand All @@ -10,6 +15,7 @@ import {
macroReplStderrContains,
} from './helpers';
import { expectStream } from '@cspotcode/expect-stream';
import { join } from 'path';

const test = context(ctxTsNode).contextEach(ctxRepl);
test.serial();
Expand Down Expand Up @@ -219,7 +225,7 @@ test.suite('top level await', ({ contextEach }) => {

expect(r.stdout).toBe('> > ');
expect(r.stderr.replace(/\r\n/g, '\n')).toBe(
'<repl>.ts(4,7): error TS2322: ' +
'<repl>.cts(4,7): error TS2322: ' +
(semver.gte(ts.version, '4.0.0')
? `Type 'number' is not assignable to type 'string'.\n`
: `Type '1' is not assignable to type 'string'.\n`) +
Expand Down Expand Up @@ -616,3 +622,17 @@ test.suite('REPL treats object literals and block scopes correctly', (test) => {
);
});
});

test('repl executes input as cjs even in esm projects', async (t) => {
// Must exec child process, because we need a different cwd.
const p = exec(`${CMD_TS_NODE_WITHOUT_PROJECT_FLAG} -i`, {
cwd: join(TEST_DIR, 'repl-in-esm-package'),
});
p.child.stdin!.write(
'import fs2 from "fs"; fs2.existsSync("does not exist")'
);
p.child.stdin!.end();
const r = await p;
expect(r.stdout).toBe('> false\n> ');
expect(r.stderr).toBe('');
});
3 changes: 3 additions & 0 deletions tests/repl-in-esm-package/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"type": "module"
}
5 changes: 5 additions & 0 deletions tests/repl-in-esm-package/tsconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
"compilerOptions": {
"module": "nodenext"
}
}