Skip to content

Commit

Permalink
Merge pull request #39 from openrewrite/fixes
Browse files Browse the repository at this point in the history
move some logic from tracing VFS code to the normal path
  • Loading branch information
garyolsen authored May 23, 2023
2 parents 274c9c8 + 28aa896 commit 34036ac
Show file tree
Hide file tree
Showing 4 changed files with 137 additions and 132 deletions.
143 changes: 74 additions & 69 deletions js/src/runtime/vfs.ts
Original file line number Diff line number Diff line change
Expand Up @@ -50,88 +50,93 @@ export function createCompilerHost(
function createVirtualFileSystem(vfsOptions: VfsOptions, allFiles: Map<string, string>) {
const system = tsvfs.createSystem(allFiles);

if (vfsOptions.traceVirtualFileSystem) {
const readFile = system.readFile;
if (readFile) {
system.readFile = function (...[filename, ...args]: Parameters<typeof readFile>) {
console.error("system.readFile(", filename, ...args, ")");
const result = readFile(filename, ...args);
if (result === undefined && allFiles.has(filename)) {
return allFiles.get(filename);
}
return result;
};
} else {
console.error("*** no system.readFile");
}
if (!system.realpath) {
// FIXME not a valid assumption
system.realpath = (path) => path;
}

const fileExists = system.fileExists;
system.fileExists = function (path) {
console.error("system.fileExists(" + path + ")");
const result = fileExists(path);
console.error("--> " + result);
if (!system.getFileSize) {
system.getFileSize = (path) => system.readFile(path)?.length ?? 0;
}

if (system.readFile) {
const oldReadFile = system.readFile;
system.readFile = (filename, ...args) => {
const result = oldReadFile(filename, ...args);
if (result === undefined && allFiles.has(filename)) {
return allFiles.get(filename);
}
return result;
};
}

const resolvePath = system.resolvePath;
system.resolvePath = function (path) {
console.error("system.resolvePath(" + path + ")");
const result = resolvePath(path);
console.error("--> " + result);
return result;
if (vfsOptions.traceVirtualFileSystem) {
addFileSystemTracing(system, allFiles);
}

return system;
}

function addFileSystemTracing(system: ts.System, allFiles: Map<string, string>) {
const readFile = system.readFile;
if (readFile) {
system.readFile = function (...[filename, ...args]: Parameters<typeof readFile>) {
console.error("system.readFile(", filename, ...args, ")");
return readFile(filename, ...args);
};
} else {
console.error("*** no system.readFile");
}

const realpath = system.realpath;
if (realpath) {
system.realpath = function (path) {
console.error("system.realpath(" + path + ")");
const result = realpath(path);
console.error("--> " + result);
return result;
};
} else {
// for resolving symlinks; assume there are none
system.realpath = function (path) {
console.error("system.realpath(" + path + ")");
const result = path;
console.error("--> " + result + " [patched]");
return result;
};
}
const fileExists = system.fileExists;
system.fileExists = function (path) {
console.error("system.fileExists(" + path + ")");
const result = fileExists(path);
console.error("--> " + result);
return result;
};

const readDirectory = system.readDirectory;
system.readDirectory = function (path, ...args) {
console.error("system.readDirectory(" + path + ")");
const result = readDirectory.call(system, path, ...args);
const resolvePath = system.resolvePath;
system.resolvePath = function (path) {
console.error("system.resolvePath(" + path + ")");
const result = resolvePath(path);
console.error("--> " + result);
return result;
};

const realpath = system.realpath;
if (realpath) {
system.realpath = function (path) {
console.error("system.realpath(" + path + ")");
const result = realpath(path);
console.error("--> " + result);
return result;
};
}

const readDirectory = system.readDirectory;
system.readDirectory = function (path, ...args) {
console.error("system.readDirectory(" + path + ")");
const result = readDirectory.call(system, path, ...args);
console.error("--> " + result);
return result;
};

const directoryExists = system.directoryExists;
system.directoryExists = function (path) {
console.error("system.directoryExists(" + path + ")");
const result = directoryExists(path);
console.error("--> " + result);
return result;
};

const directoryExists = system.directoryExists;
system.directoryExists = function (path) {
console.error("system.directoryExists(" + path + ")");
const result = directoryExists(path);
const getFileSize = system.getFileSize;
if (getFileSize) {
system.getFileSize = function (path) {
console.error("system.getFileSize(" + path + ")");
const result = getFileSize(path);
console.error("--> " + result);
return result;
};

const getFileSize = system.getFileSize;
if (getFileSize) {
system.getFileSize = function (path) {
console.error("system.getFileSize(" + path + ")");
const result = getFileSize(path);
console.error("--> " + result);
return result;
};
} else {
system.getFileSize = function (path) {
console.error("system.getFileSize(" + path + ")");
const result = readFile(path)?.length ?? 0;
console.error("--> " + result + " [patched]");
return result;
};
}
}

return system;
}
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ public static TSCRuntime init(boolean forceWrappedV8Runtime) {
: V8Host.getV8Instance().createV8Runtime();
JavetStandardConsoleInterceptor javetStandardConsoleInterceptor = new JavetStandardConsoleInterceptor(v8Runtime);
javetStandardConsoleInterceptor.register(v8Runtime.getGlobalObject());
return new TSCRuntime(v8Runtime, javetStandardConsoleInterceptor).enableVirtualFileSystemTracing();
return new TSCRuntime(v8Runtime, javetStandardConsoleInterceptor);
} catch (
JavetException e) {
throw new RuntimeException(e);
Expand Down
119 changes: 58 additions & 61 deletions src/main/resources/tsc/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -138063,73 +138063,70 @@ function $651fde053848d882$export$2321a5753084cab4(vfsOptions, compilerOptions,
}
function $651fde053848d882$var$createVirtualFileSystem(vfsOptions, allFiles) {
const system = $0fec88595207f766$export$b06ab6dfae3e7e3(allFiles);
if (vfsOptions.traceVirtualFileSystem) {
const readFile = system.readFile;
if (readFile) system.readFile = function(...[filename, ...args]) {
console.error("system.readFile(", filename, ...args, ")");
const result = readFile(filename, ...args);
if (!system.realpath) // FIXME not a valid assumption
system.realpath = (path)=>path;
if (!system.getFileSize) system.getFileSize = (path)=>system.readFile(path)?.length ?? 0;
if (system.readFile) {
const oldReadFile = system.readFile;
system.readFile = (filename, ...args)=>{
const result = oldReadFile(filename, ...args);
if (result === undefined && allFiles.has(filename)) return allFiles.get(filename);
return result;
};
else console.error("*** no system.readFile");
const fileExists = system.fileExists;
system.fileExists = function(path) {
console.error("system.fileExists(" + path + ")");
const result = fileExists(path);
console.error("--> " + result);
return result;
};
const resolvePath = system.resolvePath;
system.resolvePath = function(path) {
console.error("system.resolvePath(" + path + ")");
const result = resolvePath(path);
console.error("--> " + result);
return result;
};
const realpath = system.realpath;
if (realpath) system.realpath = function(path) {
console.error("system.realpath(" + path + ")");
const result = realpath(path);
console.error("--> " + result);
return result;
};
else // for resolving symlinks; assume there are none
system.realpath = function(path) {
console.error("system.realpath(" + path + ")");
const result = path;
console.error("--> " + result + " [patched]");
return result;
};
const readDirectory = system.readDirectory;
system.readDirectory = function(path, ...args) {
console.error("system.readDirectory(" + path + ")");
const result = readDirectory.call(system, path, ...args);
console.error("--> " + result);
return result;
};
const directoryExists = system.directoryExists;
system.directoryExists = function(path) {
console.error("system.directoryExists(" + path + ")");
const result = directoryExists(path);
console.error("--> " + result);
return result;
};
const getFileSize = system.getFileSize;
if (getFileSize) system.getFileSize = function(path) {
console.error("system.getFileSize(" + path + ")");
const result = getFileSize(path);
console.error("--> " + result);
return result;
};
else system.getFileSize = function(path) {
console.error("system.getFileSize(" + path + ")");
const result = readFile(path)?.length ?? 0;
console.error("--> " + result + " [patched]");
return result;
};
}
if (vfsOptions.traceVirtualFileSystem) $651fde053848d882$var$addFileSystemTracing(system, allFiles);
return system;
}
function $651fde053848d882$var$addFileSystemTracing(system, allFiles) {
const readFile = system.readFile;
if (readFile) system.readFile = function(...[filename, ...args]) {
console.error("system.readFile(", filename, ...args, ")");
return readFile(filename, ...args);
};
else console.error("*** no system.readFile");
const fileExists = system.fileExists;
system.fileExists = function(path) {
console.error("system.fileExists(" + path + ")");
const result = fileExists(path);
console.error("--> " + result);
return result;
};
const resolvePath = system.resolvePath;
system.resolvePath = function(path) {
console.error("system.resolvePath(" + path + ")");
const result = resolvePath(path);
console.error("--> " + result);
return result;
};
const realpath = system.realpath;
if (realpath) system.realpath = function(path) {
console.error("system.realpath(" + path + ")");
const result = realpath(path);
console.error("--> " + result);
return result;
};
const readDirectory = system.readDirectory;
system.readDirectory = function(path, ...args) {
console.error("system.readDirectory(" + path + ")");
const result = readDirectory.call(system, path, ...args);
console.error("--> " + result);
return result;
};
const directoryExists = system.directoryExists;
system.directoryExists = function(path) {
console.error("system.directoryExists(" + path + ")");
const result = directoryExists(path);
console.error("--> " + result);
return result;
};
const getFileSize = system.getFileSize;
if (getFileSize) system.getFileSize = function(path) {
console.error("system.getFileSize(" + path + ")");
const result = getFileSize(path);
console.error("--> " + result);
return result;
};
}


const $9b23dd5a3dba5d5e$var$OPEN_REWRITE_ID_SYMBOL = Symbol("OpenRewriteId");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -433,5 +433,8 @@ function test() {
);
}


@Test
public void testEmptyFile() {
parseSingleSource("", (root, ctx) -> {});
}
}

0 comments on commit 34036ac

Please sign in to comment.