Skip to content
This repository was archived by the owner on Oct 16, 2020. It is now read-only.

Fix global type loading on Windows paths #362

Merged
merged 10 commits into from
Oct 9, 2017
Merged
33 changes: 15 additions & 18 deletions src/project-manager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -343,7 +343,7 @@ export class ProjectConfiguration {
const options = configParseResult.options
const pathResolver = /^[a-z]:\//i.test(base) ? path.win32 : path.posix
this.typeRoots = options.typeRoots ?
options.typeRoots.map((r: string) => pathResolver.resolve(this.rootFilePath, r)) :
options.typeRoots.map((r: string) => toUnixPath(pathResolver.resolve(this.rootFilePath, r))) :
[]

if (/(^|\/)jsconfig\.json$/.test(this.configFilePath)) {
Expand Down Expand Up @@ -405,7 +405,7 @@ export class ProjectConfiguration {
*/
public isExpectedDeclarationFile(fileName: string): boolean {
return isDeclarationFile(fileName) &&
(this.expectedFilePaths.has(toUnixPath(fileName)) ||
(this.expectedFilePaths.has(fileName) ||
this.typeRoots.some(root => fileName.startsWith(root)))
}

Expand All @@ -427,8 +427,9 @@ export class ProjectConfiguration {
// Add all global declaration files from the workspace and all declarations from the project
for (const uri of this.fs.uris()) {
const fileName = uri2path(uri)
if (isGlobalTSFile(fileName) ||
this.isExpectedDeclarationFile(fileName)) {
const unixPath = toUnixPath(fileName)
if (isGlobalTSFile(unixPath) ||
this.isExpectedDeclarationFile(unixPath)) {
const sourceFile = program.getSourceFile(fileName)
if (!sourceFile) {
this.getHost().addFile(fileName)
Expand Down Expand Up @@ -588,7 +589,7 @@ export class ProjectManager implements Disposable {

// Create catch-all fallback configs in case there are no tsconfig.json files
// They are removed once at least one tsconfig.json is found
const trimmedRootPath = this.rootPath.replace(/\/+$/, '')
const trimmedRootPath = this.rootPath.replace(/[\\\/]+$/, '')
const fallbackConfigs: {js?: ProjectConfiguration, ts?: ProjectConfiguration} = {}
for (const configType of ['js', 'ts'] as ConfigType[]) {
const configs = this.configs[configType]
Expand Down Expand Up @@ -621,13 +622,8 @@ export class ProjectManager implements Disposable {
.filter(([uri, content]) => !!content && /\/[tj]sconfig\.json/.test(uri) && !uri.includes('/node_modules/'))
.subscribe(([uri, content]) => {
const filePath = uri2path(uri)
let dir = toUnixPath(filePath)
const pos = dir.lastIndexOf('/')
if (pos <= 0) {
dir = ''
} else {
dir = dir.substring(0, pos)
}
const pos = filePath.includes('\\') ? filePath.lastIndexOf('\\') : filePath.lastIndexOf('/')
const dir = pos <= 0 ? '' : filePath.substring(0, pos)
const configType = this.getConfigurationType(filePath)
const configs = this.configs[configType]
configs.set(dir, new ProjectConfiguration(
Expand Down Expand Up @@ -832,7 +828,7 @@ export class ProjectManager implements Disposable {
return traceObservable('Ensure config dependencies', childOf, span => {
if (!this.ensuredConfigDependencies) {
this.ensuredConfigDependencies = observableFromIterable(this.inMemoryFs.uris())
.filter(uri => this.isConfigDependency(uri2path(uri)))
.filter(uri => this.isConfigDependency(toUnixPath(uri2path(uri))))
.mergeMap(uri => this.updater.ensure(uri))
.do(noop, err => {
this.ensuredConfigDependencies = undefined
Expand Down Expand Up @@ -929,19 +925,19 @@ export class ProjectManager implements Disposable {
* @return closest configuration for a given file path or undefined if there is no such configuration
*/
public getConfigurationIfExists(filePath: string, configType = this.getConfigurationType(filePath)): ProjectConfiguration | undefined {
let dir = toUnixPath(filePath)
let dir = filePath
let config: ProjectConfiguration | undefined
const configs = this.configs[configType]
if (!configs) {
return undefined
}
const rootPath = this.rootPath.replace(/\/+$/, '')
const rootPath = this.rootPath.replace(/[\\\/]+$/, '')
while (dir && dir !== rootPath) {
config = configs.get(dir)
if (config) {
return config
}
const pos = dir.lastIndexOf('/')
const pos = dir.includes('\\') ? dir.lastIndexOf('\\') : dir.lastIndexOf('/')
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'd use string.search()

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Definitely more compact:

filePath.search(/[\\\/][^\\\/]*$/)

Do you prefer this? If so, I propose storing the regex as a static member somewhere (or adding this functionality to util.ts.

if (pos <= 0) {
dir = ''
} else {
Expand Down Expand Up @@ -1029,13 +1025,14 @@ export class ProjectManager implements Disposable {
* @return configuration type to use for a given file
*/
private getConfigurationType(filePath: string): ConfigType {
const name = path.posix.basename(filePath)
const unixPath = toUnixPath(filePath)
const name = path.posix.basename(unixPath)
if (name === 'tsconfig.json') {
return 'ts'
} else if (name === 'jsconfig.json') {
return 'js'
}
const extension = path.posix.extname(filePath)
const extension = path.posix.extname(unixPath)
if (extension === '.js' || extension === '.jsx') {
return 'js'
}
Expand Down
Loading