swc-project / swc-node

Faster ts-node without typecheck
MIT License
1.69k stars 69 forks source link

Programmatic options passed to `register` are always ignored. #716

Closed eliellis closed 8 months ago

eliellis commented 1 year ago

Due to the below if condition, introduced in #694, if options are passed to register programmatically (e.g. register({ ...customOptions })), they will be ignored in favor of either the results from readDefaultTsConfig or options gathered directly by swc from a nearby .swcrc file when process.env.SWCRC = 'true'.

https://github.com/swc-project/swc-node/blob/ba8f60db2106a84ee02b79a8b0b71ae8e1e43069/packages/register/register.ts#L108-L110

In cases where SWCRC is not true (unset, etc.), the configuration returned from readDefaultTsConfig is taken even if options are passed to register, meaning the passed options are altogether ignored.

I believe in this specific circumstance, the intuition would be for options passed to register to be used $iif$ they were given && !process.env.SWCRC.

Unless I am totally off-base here, I think the body of the if can be changed to (or something similar):

if (!process.env.SWCRC) { 
   options = Object.keys(options).length ? options : readDefaultTsConfig() 
} 

Where when process.env.SWCRC is falsy, we prefer any programmatically specified options before trying to read a tsconfig.json file. With this, I think the scenario above should be accounted for appropriately, while also not breaking what was fixed in #694.


For anyone running into this currently, using patch-package with the below diff, I was able to quickly fix this for 1.6.6

diff --git a/node_modules/@swc-node/register/lib/register.js b/node_modules/@swc-node/register/lib/register.js
index 7d4cfd8..1a8eab1 100644
--- a/node_modules/@swc-node/register/lib/register.js
+++ b/node_modules/@swc-node/register/lib/register.js
@@ -88,7 +88,7 @@ function compile(sourcecode, filename, options, async = false) {
 exports.compile = compile;
 function register(options = {}, hookOpts = {}) {
     if (!process.env.SWCRC) {
-        options = (0, read_default_tsconfig_1.readDefaultTsConfig)();
+        options = Object.keys(options).length ? options : (0, read_default_tsconfig_1.readDefaultTsConfig)();
     }
     options.module = ts.ModuleKind.CommonJS;
     (0, sourcemap_support_1.installSourceMapSupport)();

A more detailed and interactive reproduction can be seen here.

wSedlacek commented 11 months ago

Thank you so much. This patch allows me to update past 1.5.4

eliellis commented 10 months ago

With the recent updates to @swc-node/core, assuming your @swc-node/register dependency is still pinned to 1.6.6, here is the new patch to keep everything working:

diff --git a/node_modules/@swc-node/register/lib/read-default-tsconfig.js b/node_modules/@swc-node/register/lib/read-default-tsconfig.js
index fb48a43..de18175 100644
--- a/node_modules/@swc-node/register/lib/read-default-tsconfig.js
+++ b/node_modules/@swc-node/register/lib/read-default-tsconfig.js
@@ -139,6 +139,7 @@ function tsCompilerOptionsToSwcConfig(options, filename) {
                 useBuiltins: true,
             }
             : undefined,
+        baseUrl: path_1.resolve(options.baseUrl ?? './'),
         paths: Object.fromEntries(Object.entries((_h = options.paths) !== null && _h !== void 0 ? _h : {}).map(([aliasKey, aliasPaths]) => {
             var _a;
             return [
diff --git a/node_modules/@swc-node/register/lib/register.js b/node_modules/@swc-node/register/lib/register.js
index 7d4cfd8..51adf8a 100644
--- a/node_modules/@swc-node/register/lib/register.js
+++ b/node_modules/@swc-node/register/lib/register.js
@@ -88,7 +88,7 @@ function compile(sourcecode, filename, options, async = false) {
 exports.compile = compile;
 function register(options = {}, hookOpts = {}) {
     if (!process.env.SWCRC) {
-        options = (0, read_default_tsconfig_1.readDefaultTsConfig)();
+        options = Object.keys(options).length ? options : (0, read_default_tsconfig_1.readDefaultTsConfig)();
     }
     options.module = ts.ModuleKind.CommonJS;
     (0, sourcemap_support_1.installSourceMapSupport)();
eliellis commented 10 months ago

I have opened #725 to address this issue. @Brooooooklyn please take a look when you can.