Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use rescript to build when available #173

Merged
merged 1 commit into from
Apr 27, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 7 additions & 2 deletions server/src/constants.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,9 +29,14 @@ export let analysisProdPath = path.join(
"rescript-editor-analysis.exe"
);

// can't use the native bsb since we might need the watcher -w flag, which is only in the js wrapper
// export let bsbPartialPath = path.join('node_modules', 'bs-platform', process.platform, 'bsb.exe');
// can't use the native bsb/rescript since we might need the watcher -w flag, which is only in the JS wrapper
export let rescriptNodePartialPath = path.join(
"node_modules",
".bin",
"rescript"
);
export let bsbNodePartialPath = path.join("node_modules", ".bin", "bsb");

export let bsbLock = ".bsb.lock";
export let bsconfigPartialPath = "bsconfig.json";
export let compilerLogPartialPath = path.join("lib", "bs", ".compiler.log");
Expand Down
12 changes: 6 additions & 6 deletions server/src/server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -150,11 +150,10 @@ let openedFile = (fileUri: string, fileContent: string) => {
// because otherwise the diagnostics info we'll display might be stale
let bsbLockPath = path.join(projectRootPath, c.bsbLock);
if (firstOpenFileOfProject && !fs.existsSync(bsbLockPath)) {
let bsbPath = path.join(projectRootPath, c.bsbNodePartialPath);
// TODO: sometime stale .bsb.lock dangling. bsb -w knows .bsb.lock is
// stale. Use that logic
// TODO: close watcher when lang-server shuts down
if (fs.existsSync(bsbPath)) {
if (utils.findNodeBuildOfProjectRoot(projectRootPath) != null) {
let payload: clientSentBuildAction = {
title: c.startBuildAction,
projectRootPath: projectRootPath,
Expand Down Expand Up @@ -540,14 +539,15 @@ function onMessage(msg: m.Message) {
) {
let msg_ = msg.result as clientSentBuildAction;
let projectRootPath = msg_.projectRootPath;
let bsbNodePath = path.join(projectRootPath, c.bsbNodePartialPath);
// TODO: sometime stale .bsb.lock dangling
// TODO: close watcher when lang-server shuts down. However, by Node's
// default, these subprocesses are automatically killed when this
// language-server process exits
if (fs.existsSync(bsbNodePath)) {
let bsbProcess = utils.runBsbWatcherUsingValidBsbNodePath(
bsbNodePath,
let found = utils.findNodeBuildOfProjectRoot(projectRootPath);
if (found != null) {
let bsbProcess = utils.runBuildWatcherUsingValidBuildPath(
found.buildPath,
found.isReScript,
projectRootPath
);
let root = projectsFiles.get(projectRootPath)!;
Expand Down
45 changes: 34 additions & 11 deletions server/src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -54,17 +54,32 @@ export let findBscNativeOfFile = (
let bscNativePath = path.join(dir, c.bscNativePartialPath);

if (fs.existsSync(bscNativeReScriptPath)) {
return bscNativeReScriptPath
return bscNativeReScriptPath;
} else if (fs.existsSync(bscNativePath)) {
return bscNativePath
return bscNativePath;
} else if (dir === source) {
// reached the top
return null
return null;
} else {
return findBscNativeOfFile(dir);
}
};

// TODO: this doesn't handle file:/// scheme
export let findNodeBuildOfProjectRoot = (
projectRootPath: p.DocumentUri
): null | { buildPath: p.DocumentUri; isReScript: boolean } => {
let rescriptNodePath = path.join(projectRootPath, c.rescriptNodePartialPath);
let bsbNodePath = path.join(projectRootPath, c.bsbNodePartialPath);

if (fs.existsSync(rescriptNodePath)) {
return { buildPath: rescriptNodePath, isReScript: true };
} else if (fs.existsSync(bsbNodePath)) {
return { buildPath: bsbNodePath, isReScript: false };
}
return null;
};

type execResult =
| {
kind: "success";
Expand Down Expand Up @@ -129,10 +144,14 @@ export let runAnalysisAfterSanityCheck = (
return JSON.parse(stdout.toString());
};

export let runBsbWatcherUsingValidBsbNodePath = (
bsbNodePath: p.DocumentUri,
export let runBuildWatcherUsingValidBuildPath = (
buildPath: p.DocumentUri,
isRescript: boolean,
projectRootPath: p.DocumentUri
) => {
let cwdEnv = {
cwd: projectRootPath,
};
if (process.platform === "win32") {
/*
- a node.js script in node_modules/.bin on windows is wrapped in a
Expand All @@ -146,13 +165,17 @@ export let runBsbWatcherUsingValidBsbNodePath = (
(since the path might have spaces), which `execFile` would have done
for you under the hood
*/
return childProcess.exec(`"${bsbNodePath}".cmd -w`, {
cwd: projectRootPath,
});
if (isRescript) {
return childProcess.exec(`"${buildPath}".cmd build -w`, cwdEnv);
} else {
return childProcess.exec(`"${buildPath}".cmd -w`, cwdEnv);
}
} else {
return childProcess.execFile(bsbNodePath, ["-w"], {
cwd: projectRootPath,
});
if (isRescript) {
return childProcess.execFile(buildPath, ["build", "-w"], cwdEnv);
} else {
return childProcess.execFile(buildPath, ["-w"], cwdEnv);
}
}
};

Expand Down