@@ -14,6 +14,7 @@ import * as c from "./constants";
14
14
import * as lookup from "./lookup" ;
15
15
import { reportError } from "./errorReporter" ;
16
16
import config from "./config" ;
17
+ import { filesDiagnostics , projectsFiles } from "./projectFiles" ;
17
18
18
19
let tempFilePrefix = "rescript_format_file_" + process . pid + "_" ;
19
20
let tempFileId = 0 ;
@@ -24,9 +25,7 @@ export let createFileInTempDir = (extension = "") => {
24
25
return path . join ( os . tmpdir ( ) , tempFileName ) ;
25
26
} ;
26
27
27
- // TODO: races here?
28
- // TODO: this doesn't handle file:/// scheme
29
- export let findProjectRootOfFile = (
28
+ let findProjectRootOfFileInDir = (
30
29
source : p . DocumentUri
31
30
) : null | p . DocumentUri => {
32
31
let dir = path . dirname ( source ) ;
@@ -40,11 +39,37 @@ export let findProjectRootOfFile = (
40
39
// reached top
41
40
return null ;
42
41
} else {
43
- return findProjectRootOfFile ( dir ) ;
42
+ return findProjectRootOfFileInDir ( dir ) ;
44
43
}
45
44
}
46
45
} ;
47
46
47
+ // TODO: races here?
48
+ // TODO: this doesn't handle file:/// scheme
49
+ export let findProjectRootOfFile = (
50
+ source : p . DocumentUri
51
+ ) : null | p . DocumentUri => {
52
+ // First look in project files
53
+ let foundRootFromProjectFiles : string | null = null ;
54
+
55
+ for ( const rootPath of projectsFiles . keys ( ) ) {
56
+ if ( source . startsWith ( rootPath ) ) {
57
+ // Prefer the longest path (most nested)
58
+ if (
59
+ foundRootFromProjectFiles == null ||
60
+ rootPath . length > foundRootFromProjectFiles . length
61
+ )
62
+ foundRootFromProjectFiles = rootPath ;
63
+ }
64
+ }
65
+
66
+ if ( foundRootFromProjectFiles != null ) {
67
+ return foundRootFromProjectFiles ;
68
+ } else {
69
+ return findProjectRootOfFileInDir ( source ) ;
70
+ }
71
+ } ;
72
+
48
73
// Check if binaryName exists inside binaryDirPath and return the joined path.
49
74
export let findBinary = (
50
75
binaryDirPath : p . DocumentUri | null ,
@@ -138,7 +163,9 @@ export let formatCode = (
138
163
}
139
164
} ;
140
165
141
- let findReScriptVersion = ( filePath : p . DocumentUri ) : string | undefined => {
166
+ export let findReScriptVersion = (
167
+ filePath : p . DocumentUri
168
+ ) : string | undefined => {
142
169
let projectRoot = findProjectRootOfFile ( filePath ) ;
143
170
if ( projectRoot == null ) {
144
171
return undefined ;
@@ -161,25 +188,31 @@ let findReScriptVersion = (filePath: p.DocumentUri): string | undefined => {
161
188
}
162
189
} ;
163
190
191
+ let binaryPath : string | null = null ;
192
+ if ( fs . existsSync ( c . analysisDevPath ) ) {
193
+ binaryPath = c . analysisDevPath ;
194
+ } else if ( fs . existsSync ( c . analysisProdPath ) ) {
195
+ binaryPath = c . analysisProdPath ;
196
+ } else {
197
+ }
198
+
164
199
export let runAnalysisAfterSanityCheck = (
165
200
filePath : p . DocumentUri ,
166
201
args : Array < any > ,
167
202
projectRequired = false
168
203
) => {
169
- let binaryPath ;
170
- if ( fs . existsSync ( c . analysisDevPath ) ) {
171
- binaryPath = c . analysisDevPath ;
172
- } else if ( fs . existsSync ( c . analysisProdPath ) ) {
173
- binaryPath = c . analysisProdPath ;
174
- } else {
204
+ if ( binaryPath == null ) {
175
205
return null ;
176
206
}
177
207
178
208
let projectRootPath = findProjectRootOfFile ( filePath ) ;
179
209
if ( projectRootPath == null && projectRequired ) {
180
210
return null ;
181
211
}
182
- let rescriptVersion = findReScriptVersion ( filePath ) ;
212
+ let rescriptVersion =
213
+ projectsFiles . get ( projectRootPath ?? "" ) ?. rescriptVersion ??
214
+ findReScriptVersion ( filePath ) ;
215
+
183
216
let options : childProcess . ExecFileSyncOptions = {
184
217
cwd : projectRootPath || undefined ,
185
218
maxBuffer : Infinity ,
@@ -449,9 +482,6 @@ let parseFileAndRange = (fileAndRange: string) => {
449
482
} ;
450
483
451
484
// main parsing logic
452
- export type filesDiagnostics = {
453
- [ key : string ] : p . Diagnostic [ ] ;
454
- } ;
455
485
type parsedCompilerLogResult = {
456
486
done : boolean ;
457
487
result : filesDiagnostics ;
0 commit comments