Skip to content

Commit

Permalink
fix(rosetta,pacmak): TypeError in node 10 with --experimental-worker (#…
Browse files Browse the repository at this point in the history
…2554)

isaacs/node-graceful-fs#204 prevents `graceful-fs` (a dependency of
`fs-extra`) from loading in a worker thread when using node < 12. The
issue affects only version `4.2.5`, so pinning both `jsii-rosetta` and
`jsii-pacmak` (both of which may use worker threads when available) to
the last known good version: `4.2.4`, until a fixed release of
`graceful-fs` is released.
  • Loading branch information
RomainMuller authored Feb 9, 2021
1 parent 5822e48 commit 4728e86
Show file tree
Hide file tree
Showing 4 changed files with 23 additions and 48 deletions.
67 changes: 20 additions & 47 deletions packages/jsii-pacmak/lib/targets/go.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { CodeMaker } from 'codemaker';
import { promises as fs } from 'fs';
import * as fs from 'fs-extra';
import { Assembly } from 'jsii-reflect';
import { Rosetta } from 'jsii-rosetta';
import * as path from 'path';
Expand Down Expand Up @@ -46,19 +46,8 @@ export class Golang extends Target {

// delete local.go.mod and local.go.sum from the output directory so it doesn't get published
const localGoSum = `${path.basename(localGoMod, '.mod')}.sum`;
await unlink(path.join(pkgDir, localGoMod));
await unlink(path.join(pkgDir, localGoSum));

async function unlink(file: string): Promise<void> {
try {
await fs.unlink(file);
} catch (err) {
// Ignore ENOENT, as this means the file already does not exist
if (err.code !== 'ENOENT') {
throw err;
}
}
}
await fs.remove(path.join(pkgDir, localGoMod));
await fs.remove(path.join(pkgDir, localGoSum));
}

/**
Expand Down Expand Up @@ -89,33 +78,27 @@ export class Golang extends Target {
// local build directory for this module. if
// we do, add a "replace" directive to point to it instead of download from
// the network.
const visit = async (pkg: RootPackage): Promise<void> => {
await Promise.all(
pkg.packageDependencies.map(async (dep) => {
await Promise.all(
dirs.map((baseDir) =>
tryFindLocalModule(baseDir, dep).then((moduleDir) => {
if (moduleDir) {
replace[dep.goModuleName] = moduleDir;
}
}),
),
);
// recurse to transitive deps ("replace" is only considered at the top level go.mod)
return visit(dep);
}),
);
const visit = (pkg: RootPackage) => {
for (const dep of pkg.packageDependencies) {
for (const baseDir of dirs) {
const moduleDir = tryFindLocalModule(baseDir, dep);
if (moduleDir) {
replace[dep.goModuleName] = moduleDir;
}
}

// recurse to transitive deps ("replace" is only considered at the top level go.mod)
visit(dep);
}
};

await visit(this.goGenerator.rootPackage);
visit(this.goGenerator.rootPackage);

// write `local.go.mod`

// read existing content
const goMod = path.join(pkgDir, GOMOD_FILENAME);
const lines = await fs
.readFile(goMod, 'utf8')
.then((text) => text.split('\n'));
const lines = (await fs.readFile(goMod, 'utf-8')).split('\n');

for (const [from, to] of Object.entries(replace)) {
logging.info(`Local replace: ${from} => ${to}`);
Expand Down Expand Up @@ -202,24 +185,14 @@ class GoGenerator implements IGenerator {
* @param baseDir the `dist/go` directory
* @returns `undefined` if not or the module directory otherwise.
*/
async function tryFindLocalModule(baseDir: string, pkg: RootPackage) {
function tryFindLocalModule(baseDir: string, pkg: RootPackage) {
const gomodPath = path.join(baseDir, pkg.packageName, GOMOD_FILENAME);
if (
!(await fs.stat(gomodPath).then(
() => true, // File exists - we could stat it!
(err) =>
err.code === 'ENOENT'
? false // ENOENT means the file does not exist...
: Promise.reject(err), // Re-throw the error!
))
) {
if (!fs.pathExistsSync(gomodPath)) {
return undefined;
}

// read `go.mod` and check that it is for the correct module
const gomod = await fs
.readFile(gomodPath, 'utf8')
.then((text) => text.split('\n'));
const gomod = fs.readFileSync(gomodPath, 'utf-8').split('\n');
const isExpectedModule = gomod.find(
(line) => line.trim() === `module ${pkg.goModuleName}`,
);
Expand Down
1 change: 1 addition & 0 deletions packages/jsii-pacmak/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@
"commonmark": "^0.29.3",
"escape-string-regexp": "^4.0.0",
"fs-extra": "^9.1.0",
"graceful-fs": "4.2.4",
"jsii-reflect": "^0.0.0",
"jsii-rosetta": "^0.0.0",
"semver": "^7.3.4",
Expand Down
1 change: 1 addition & 0 deletions packages/jsii-rosetta/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@
"@jsii/spec": "^0.0.0",
"commonmark": "^0.29.3",
"fs-extra": "^9.1.0",
"graceful-fs": "4.2.4",
"typescript": "~3.9.7",
"xmldom": "^0.4.0",
"yargs": "^16.2.0"
Expand Down
2 changes: 1 addition & 1 deletion yarn.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 4728e86

Please sign in to comment.