Skip to content

Tweak heuristics to find the project root and "build root" #16

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

Closed
wants to merge 2 commits into from
Closed
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
30 changes: 15 additions & 15 deletions server/src/server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -126,33 +126,33 @@ let openedFile = (fileUri: string, fileContent: string) => {

stupidFileContentCache.set(filePath, fileContent);

let projectRootPath = utils.findProjectRootOfFile(filePath);
if (projectRootPath != null) {
if (!projectsFiles.has(projectRootPath)) {
projectsFiles.set(projectRootPath, {
let buildRootPath = utils.findBuildRootOfFile(filePath);
if (buildRootPath != null) {
if (!projectsFiles.has(buildRootPath)) {
projectsFiles.set(buildRootPath, {
openFiles: new Set(),
filesWithDiagnostics: new Set(),
bsbWatcherByEditor: null,
});
compilerLogsWatcher.add(
path.join(projectRootPath, c.compilerLogPartialPath)
path.join(buildRootPath, c.compilerLogPartialPath)
);
}
let root = projectsFiles.get(projectRootPath)!;
let root = projectsFiles.get(buildRootPath)!;
root.openFiles.add(filePath);
let firstOpenFileOfProject = root.openFiles.size === 1;
// check if .bsb.lock is still there. If not, start a bsb -w ourselves
// because otherwise the diagnostics info we'll display might be stale
let bsbLockPath = path.join(projectRootPath, c.bsbLock);
let bsbLockPath = path.join(buildRootPath, c.bsbLock);
if (firstOpenFileOfProject && !fs.existsSync(bsbLockPath)) {
let bsbPath = path.join(projectRootPath, c.bsbPartialPath);
let bsbPath = path.join(buildRootPath, c.bsbPartialPath);
// 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)) {
let payload: clientSentBuildAction = {
title: c.startBuildAction,
projectRootPath: projectRootPath,
projectRootPath: buildRootPath,
};
let params = {
type: p.MessageType.Info,
Expand Down Expand Up @@ -183,17 +183,17 @@ let closedFile = (fileUri: string) => {

stupidFileContentCache.delete(filePath);

let projectRootPath = utils.findProjectRootOfFile(filePath);
if (projectRootPath != null) {
let root = projectsFiles.get(projectRootPath);
let buildRootPath = utils.findBuildRootOfFile(filePath);
if (buildRootPath != null) {
let root = projectsFiles.get(buildRootPath);
if (root != null) {
root.openFiles.delete(filePath);
// clear diagnostics too if no open files open in said project
if (root.openFiles.size === 0) {
compilerLogsWatcher.unwatch(
path.join(projectRootPath, c.compilerLogPartialPath)
path.join(buildRootPath, c.compilerLogPartialPath)
);
deleteProjectDiagnostics(projectRootPath);
deleteProjectDiagnostics(buildRootPath);
if (root.bsbWatcherByEditor !== null) {
root.bsbWatcherByEditor.kill();
root.bsbWatcherByEditor = null;
Expand Down Expand Up @@ -419,7 +419,7 @@ process.on("message", (msg: m.Message) => {
if (projectRootPath == null) {
let params: p.ShowMessageParams = {
type: p.MessageType.Error,
message: `Cannot find a nearby ${c.bsconfigPartialPath}. It's needed for determining the project's root.`,
message: `Cannot find a nearby ${c.bscPartialPath}. It's needed for determining the project's root.`,
};
let response: m.NotificationMessage = {
jsonrpc: c.jsonrpcVersion,
Expand Down
20 changes: 19 additions & 1 deletion server/src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ export let findProjectRootOfFile = (
source: p.DocumentUri
): null | p.DocumentUri => {
let dir = path.dirname(source);
if (fs.existsSync(path.join(dir, c.bsconfigPartialPath))) {
if (fs.existsSync(path.join(dir, c.bscPartialPath))) {
return dir;
} else {
if (dir === source) {
Expand All @@ -34,6 +34,24 @@ export let findProjectRootOfFile = (
}
};

// the "build root" represents the nearest directory containing a "bsconfig.json" file.
// "bsconfig.json" can be used to locate the nearest build artefacts
export let findBuildRootOfFile = (
source: p.DocumentUri
): null | p.DocumentUri => {
let dir = path.dirname(source);
if (fs.existsSync(path.join(dir, c.bsconfigPartialPath))) {
return dir;
} else {
if (dir === source) {
// reached top
return null;
} else {
return findBuildRootOfFile(dir);
}
}
};

type execResult =
| {
kind: "success";
Expand Down