Skip to content

Commit 57009e5

Browse files
author
Iwan
committed
Split project from from build root.
the "build root" represents the nearest directory containing a "bsconfig.json" file. "bsconfig.json" can be used to locate the nearest build artefacts and .compiler.log for diagnostics.
1 parent c04b704 commit 57009e5

File tree

2 files changed

+32
-14
lines changed

2 files changed

+32
-14
lines changed

server/src/server.ts

+14-14
Original file line numberDiff line numberDiff line change
@@ -126,33 +126,33 @@ let openedFile = (fileUri: string, fileContent: string) => {
126126

127127
stupidFileContentCache.set(filePath, fileContent);
128128

129-
let projectRootPath = utils.findProjectRootOfFile(filePath);
130-
if (projectRootPath != null) {
131-
if (!projectsFiles.has(projectRootPath)) {
132-
projectsFiles.set(projectRootPath, {
129+
let buildRootPath = utils.findBuildRootOfFile(filePath);
130+
if (buildRootPath != null) {
131+
if (!projectsFiles.has(buildRootPath)) {
132+
projectsFiles.set(buildRootPath, {
133133
openFiles: new Set(),
134134
filesWithDiagnostics: new Set(),
135135
bsbWatcherByEditor: null,
136136
});
137137
compilerLogsWatcher.add(
138-
path.join(projectRootPath, c.compilerLogPartialPath)
138+
path.join(buildRootPath, c.compilerLogPartialPath)
139139
);
140140
}
141-
let root = projectsFiles.get(projectRootPath)!;
141+
let root = projectsFiles.get(buildRootPath)!;
142142
root.openFiles.add(filePath);
143143
let firstOpenFileOfProject = root.openFiles.size === 1;
144144
// check if .bsb.lock is still there. If not, start a bsb -w ourselves
145145
// because otherwise the diagnostics info we'll display might be stale
146-
let bsbLockPath = path.join(projectRootPath, c.bsbLock);
146+
let bsbLockPath = path.join(buildRootPath, c.bsbLock);
147147
if (firstOpenFileOfProject && !fs.existsSync(bsbLockPath)) {
148-
let bsbPath = path.join(projectRootPath, c.bsbPartialPath);
148+
let bsbPath = path.join(buildRootPath, c.bsbPartialPath);
149149
// TODO: sometime stale .bsb.lock dangling. bsb -w knows .bsb.lock is
150150
// stale. Use that logic
151151
// TODO: close watcher when lang-server shuts down
152152
if (fs.existsSync(bsbPath)) {
153153
let payload: clientSentBuildAction = {
154154
title: c.startBuildAction,
155-
projectRootPath: projectRootPath,
155+
projectRootPath: buildRootPath,
156156
};
157157
let params = {
158158
type: p.MessageType.Info,
@@ -183,17 +183,17 @@ let closedFile = (fileUri: string) => {
183183

184184
stupidFileContentCache.delete(filePath);
185185

186-
let projectRootPath = utils.findProjectRootOfFile(filePath);
187-
if (projectRootPath != null) {
188-
let root = projectsFiles.get(projectRootPath);
186+
let buildRootPath = utils.findBuildRootOfFile(filePath);
187+
if (buildRootPath != null) {
188+
let root = projectsFiles.get(buildRootPath);
189189
if (root != null) {
190190
root.openFiles.delete(filePath);
191191
// clear diagnostics too if no open files open in said project
192192
if (root.openFiles.size === 0) {
193193
compilerLogsWatcher.unwatch(
194-
path.join(projectRootPath, c.compilerLogPartialPath)
194+
path.join(buildRootPath, c.compilerLogPartialPath)
195195
);
196-
deleteProjectDiagnostics(projectRootPath);
196+
deleteProjectDiagnostics(buildRootPath);
197197
if (root.bsbWatcherByEditor !== null) {
198198
root.bsbWatcherByEditor.kill();
199199
root.bsbWatcherByEditor = null;

server/src/utils.ts

+18
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,24 @@ export let findProjectRootOfFile = (
3434
}
3535
};
3636

37+
// the "build root" represents the nearest directory containing a "bsconfig.json" file.
38+
// "bsconfig.json" can be used to locate the nearest build artefacts
39+
export let findBuildRootOfFile = (
40+
source: p.DocumentUri
41+
): null | p.DocumentUri => {
42+
let dir = path.dirname(source);
43+
if (fs.existsSync(path.join(dir, c.bsconfigPartialPath))) {
44+
return dir;
45+
} else {
46+
if (dir === source) {
47+
// reached top
48+
return null;
49+
} else {
50+
return findBuildRootOfFile(dir);
51+
}
52+
}
53+
};
54+
3755
type execResult =
3856
| {
3957
kind: "success";

0 commit comments

Comments
 (0)