Skip to content

Commit

Permalink
Fix several path-related issues that manifested in the web interface
Browse files Browse the repository at this point in the history
1. .realpath() in BrowserFS seems to not do the right thing (?) .resolve()
seems to be the right thing. Use that.

2. In getBaseDir when buildPath and source had an identical prefix and never
differed, we were not cutting anything off, which was wrong. So set
`cutoffIndex` to the length if we don't find a differing spot in the loop.

3. In node, the '.js' extension is at least optional, and maybe
required/generated by some TS stuff. So make our requires allow for/do that as
well.
  • Loading branch information
jpolitz committed Jun 17, 2021
1 parent 2d76437 commit 21ceace
Show file tree
Hide file tree
Showing 5 changed files with 24 additions and 5 deletions.
1 change: 0 additions & 1 deletion src/arr/compiler/js-of-pyret.arr
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ import string-dict as SD
import pprint as PP

import file("anf.arr") as N
import file("anf-loop-compiler.arr") as AL
import file("ast-util.arr") as AU
import file("compile-structs.arr") as C
import file("concat-lists.arr") as CL
Expand Down
1 change: 1 addition & 0 deletions src/arr/compiler/ts-direct-codegen-impl.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1558,6 +1558,7 @@ import type { Variant, PyretObject } from './ts-codegen-helpers';
for(let i = 0; i < shorter; i += 1) {
if(sourcePath[i] !== buildDir[i]) { cutoffIndex = i; break; }
}
if(cutoffIndex === 0) { cutoffIndex = buildDir.length; }
return [ buildDir.substring(0, cutoffIndex), sourcePath ];
}

Expand Down
2 changes: 1 addition & 1 deletion src/webworker/control.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ export const installFileSystem = () => {
};

export const loadBuiltins = (): void => {
load(bfsSetup.fs, path.compileBuiltinJS, path.uncompiled, runtimeFiles);
load(bfsSetup.fs, bfsSetup.path, path.compileBuiltinJS, path.uncompiled, runtimeFiles);
};

export const { runProgram } = backend;
Expand Down
17 changes: 14 additions & 3 deletions src/webworker/runner.ts
Original file line number Diff line number Diff line change
Expand Up @@ -186,15 +186,21 @@ export const makeRequireAsync = (basePath: string, rtCfg?: RuntimeConfig): ((imp
if (importPath in nodeModules) {
return (nodeModules as any)[importPath];
}
const nextPath = path.join(requiringWd, importPath);
let nextPath = path.join(requiringWd, importPath);
// NOTE(joe, June 2021):
// Required, because some TypeScript/es module workflows have you write the module without the .js
if(nextPath.slice(-3) !== ".js") {
nextPath = nextPath + ".js";
}

const cwd = path.parse(nextPath).dir;
const stoppedPath = `${nextPath}.stopped`;
// Get the absolute path to uniquely identify modules
// Cache modules based upon the absolute path for singleton modules
const cachePath = path.resolve(stoppedPath);
if (cachePath in cache) { return cache[cachePath]; }
if (!fs.existsSync(nextPath)) {
throw new Error(`Path did not exist in requireSync: ${nextPath}`);
throw new Error(`Path did not exist in requireASync: ${nextPath}`);
}
currentRunner.pauseK((kontinue: (result: any) => void) => {
const lastPath = currentRunner.path;
Expand Down Expand Up @@ -269,7 +275,12 @@ export const makeRequire = (basePath: string, rtCfg?: RuntimeConfig): ((importPa
return (nodeModules as any)[importPath];
}
const oldWd = cwd;
const nextPath = path.join(cwd, importPath);
let nextPath = path.join(cwd, importPath);
// NOTE(joe, June 2021):
// Required, because some TypeScript/es module workflows have you write the module without the .js
if (nextPath.slice(-3) !== ".js") {
nextPath = nextPath + ".js";
}
if (nextPath in cache) { return cache[nextPath]; }

if (isRoot) {
Expand Down
8 changes: 8 additions & 0 deletions src/webworker/runtime-loader.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import { compiled, compiledBuiltin } from './path';

export default function load(
fs: any,
path: any,
prewrittenDirectory: string,
uncompiledDirectory: string,
runtimeFiles: any,
Expand Down Expand Up @@ -29,10 +30,17 @@ export default function load(
const statResult = fs.statSync(fullPathKey);
const localTimestamp = statResult.mtime.getTime();
if (localTimestamp < timestamp) {
if (!fs.existsSync(path.dirname(compiledPath))) {
fs.mkdirSync(path.dirname(compiledPath));
}
fs.writeFileSync(fullPathKey, content);
fs.writeFileSync(compiledPath, content);
}
} else {
if (!fs.existsSync(path.dirname(fullPathKey))) {
fs.mkdirSync(path.dirname(fullPathKey));
fs.mkdirSync(path.dirname(compiledPath));
}
fs.writeFileSync(fullPathKey, content);
fs.writeFileSync(compiledPath, content);
}
Expand Down

0 comments on commit 21ceace

Please sign in to comment.