Skip to content

Commit 737ffad

Browse files
hanslKeen Yee Liau
authored and
Keen Yee Liau
committed
ci: bootstrap-local now prioritizes typescript files over JSON
The require() logic by default is not enough; require("file") would pick the json if there are both a file.ts and file.json. This does not show up in prod because js files are prioritized over json, but it does show up in bootstrap because we compile ts in memory and there are no .js file on disk (which require() could potentially see). This is necessary for the schema change because schema.json and schema.ts collides. We should also rename the commands from *-impl.ts to just *.ts (they were named that way because of this conflict).
1 parent f34dfc9 commit 737ffad

File tree

1 file changed

+11
-1
lines changed

1 file changed

+11
-1
lines changed

lib/bootstrap-local.js

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -126,7 +126,17 @@ if (!__dirname.match(new RegExp(`\\${path.sep}node_modules\\${path.sep}`))) {
126126
const p = path.join(packages[match].root, request.substr(match.length));
127127
return oldResolve.call(this, p, parent);
128128
} else {
129-
return oldResolve.apply(this, arguments);
129+
// Because loading `.ts` ends up AFTER `.json` in the require() logic, requiring a file that has both `.json`
130+
// and `.ts` versions will only get the `.json` content (which wouldn't happen if the .ts was compiled to
131+
// JavaScript). We load `.ts` files first here to avoid this conflict. It's hacky, but so is the rest of this
132+
// file.
133+
const resolvedPath = oldResolve.apply(this, arguments);
134+
const maybeTsPath = resolvedPath.endsWith('.json') && resolvedPath.replace(/\.json$/, '.ts');
135+
if (maybeTsPath && !request.endsWith('.json') && fs.existsSync(maybeTsPath)) {
136+
return maybeTsPath;
137+
}
138+
139+
return resolvedPath;
130140
}
131141
}
132142
};

0 commit comments

Comments
 (0)