Skip to content

Commit

Permalink
Normalize URIs for windows and remove back slashes of relative paths
Browse files Browse the repository at this point in the history
  • Loading branch information
charlespwd committed Oct 15, 2024
1 parent 2e4bf7d commit 35b07d1
Show file tree
Hide file tree
Showing 14 changed files with 56 additions and 55 deletions.
16 changes: 11 additions & 5 deletions packages/theme-check-common/src/path.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,20 +2,26 @@ import { RelativePath, Uri } from './types';
import { URI, Utils } from 'vscode-uri';

export function relative(uri: Uri, rootUri: Uri): RelativePath {
return uri.replace(rootUri, '').replace(/^\/+/, '');
return uri
.replace(rootUri, '')
.replace(/\\\\/g, '/') // We expect forward slash paths (windows path get normalized)
.replace(/^\/+/, '');
}

export function join(rootUri: Uri, ...paths: string[]): string {
const root = URI.parse(rootUri);
return Utils.joinPath(root, ...paths).toString();
return normalize(Utils.joinPath(root, ...paths));
}

export function normalize(uri: Uri): Uri {
return URI.parse(uri).toString();
export function normalize(uri: Uri | URI): Uri {
if (!URI.isUri(uri)) {
uri = URI.parse(uri);
}
return uri.toString(true);
}

export function dirname(uri: Uri): Uri {
return Utils.dirname(URI.parse(uri)).toString();
return normalize(Utils.dirname(URI.parse(uri)));
}

export function fsPath(uri: Uri): string {
Expand Down
3 changes: 2 additions & 1 deletion packages/theme-check-node/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import {
Theme,
toSourceCode as commonToSourceCode,
check as coreCheck,
path as pathUtils,
isIgnored,
parseJSON,
} from '@shopify/theme-check-common';
Expand Down Expand Up @@ -44,7 +45,7 @@ export async function toSourceCode(
): Promise<LiquidSourceCode | JSONSourceCode | undefined> {
try {
const source = await fs.readFile(absolutePath, 'utf8');
return commonToSourceCode(URI.file(absolutePath).toString(), source);
return commonToSourceCode(pathUtils.normalize(URI.file(absolutePath)), source);
} catch (e) {
return undefined;
}
Expand Down
14 changes: 7 additions & 7 deletions packages/theme-language-server-common/src/TypeSystem.spec.ts
Original file line number Diff line number Diff line change
@@ -1,16 +1,16 @@
import { beforeEach, describe, vi, it, expect, assert } from 'vitest';
import { SettingsSchemaJSONFile } from './settings';
import { ArrayType, TypeSystem } from './TypeSystem';
import {
AssignMarkup,
LiquidHtmlNode,
LiquidVariableOutput,
NamedTags,
NodeTypes,
toLiquidHtmlAST,
} from '@shopify/liquid-html-parser';
import { isLiquidVariableOutput, isNamedLiquidTag } from './utils';
import { path as pathUtils } from '@shopify/theme-check-common';
import { assert, beforeEach, describe, expect, it, vi } from 'vitest';
import { URI } from 'vscode-uri';
import { SettingsSchemaJSONFile } from './settings';
import { ArrayType, TypeSystem } from './TypeSystem';
import { isLiquidVariableOutput, isNamedLiquidTag } from './utils';

describe('Module: TypeSystem', () => {
let typeSystem: TypeSystem;
Expand Down Expand Up @@ -329,14 +329,14 @@ describe('Module: TypeSystem', () => {
variableOutput.markup,
ast,
// This will be different on Windows ^^
URI.from({ scheme: 'file', path }).toString(),
pathUtils.normalize(URI.from({ scheme: 'file', path })),
);
expect(inferredType).to.eql(object);
inferredType = await typeSystem.inferType(
variableOutput.markup,
ast,
// This will be different on Windows ^^
URI.from({ scheme: 'file', path: 'file.liquid' }).toString(),
pathUtils.normalize(URI.from({ scheme: 'file', path: 'file.liquid' })),
);
expect(inferredType).to.eql('untyped');
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
import { describe, it, expect, vi, beforeEach } from 'vitest';
import { URI } from 'vscode-uri';
import { Offense, SourceCodeType, Severity } from '@shopify/theme-check-common';
import { Offense, SourceCodeType, Severity, path } from '@shopify/theme-check-common';
import { DiagnosticsManager } from '../../diagnostics';
import { DocumentManager } from '../../documents';
import { FixAllProvider } from './FixAllProvider';
import { TextDocument } from 'vscode-languageserver-textdocument';

describe('Unit: FixAllProvider', () => {
const uri = URI.file('/path/to/file.liquid').toString();
const uri = path.normalize(URI.file('/path/to/file.liquid'));
const contents = `
{% assign x = 1 %}
<script src="2.js"></script>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
import { describe, it, expect, vi, beforeEach } from 'vitest';
import { URI } from 'vscode-uri';
import { Offense, SourceCodeType, Severity } from '@shopify/theme-check-common';
import { Offense, SourceCodeType, Severity, path } from '@shopify/theme-check-common';
import { DiagnosticsManager } from '../../diagnostics';
import { DocumentManager } from '../../documents';
import { FixProvider } from './FixProvider';
import { TextDocument } from 'vscode-languageserver-textdocument';

describe('Unit: FixProvider', () => {
const uri = URI.file('/path/to/file.liquid').toString();
const uri = path.normalize(URI.file('/path/to/file.liquid'));
const contents = `
{% assign x = 1 %}
<script src="2.js"></script>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
import { describe, it, expect, vi, beforeEach } from 'vitest';
import { Offense, Severity, SourceCodeType, Suggestion, path } from '@shopify/theme-check-common';
import { beforeEach, describe, expect, it, vi } from 'vitest';
import { TextDocument } from 'vscode-languageserver-textdocument';
import { URI } from 'vscode-uri';
import { Offense, SourceCodeType, Severity, Suggestion } from '@shopify/theme-check-common';
import { DiagnosticsManager } from '../../diagnostics';
import { DocumentManager } from '../../documents';
import { SuggestionProvider } from './SuggestionProvider';
import { TextDocument } from 'vscode-languageserver-textdocument';

describe('Unit: SuggestionProvider', () => {
const uri = URI.file('/path/to/file.liquid').toString();
const uri = path.normalize(URI.file('/path/to/file.liquid'));
const contents = `
{% assign x = 1 %}
<script src="2.js"></script>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
import { describe, it, expect, vi, beforeEach, Mock } from 'vitest';
import { Offense, path, Position, Severity, SourceCodeType } from '@shopify/theme-check-common';
import { beforeEach, describe, expect, it, Mock, vi } from 'vitest';
import { TextDocument } from 'vscode-languageserver-textdocument';
import { URI } from 'vscode-uri';
import { Offense, SourceCodeType, Severity, Position } from '@shopify/theme-check-common';
import { DiagnosticsManager } from '../../diagnostics';
import { offenseToDiagnostic } from '../../diagnostics/offenseToDiagnostic';
import { DocumentManager } from '../../documents';
import { ApplyFixesProvider } from './ApplyFixesProvider';
import { TextDocument } from 'vscode-languageserver-textdocument';
import { offenseToDiagnostic } from '../../diagnostics/offenseToDiagnostic';

describe('Unit: ApplyFixesProvider', () => {
const uri = URI.file('/path/to/file.liquid').toString();
const uri = path.normalize(URI.file('/path/to/file.liquid'));
const contents = `
{% assign x = 1 %}
<script src="2.js"></script>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import {
Severity,
Position,
Suggestion,
path,
} from '@shopify/theme-check-common';
import { DiagnosticsManager } from '../../diagnostics';
import { DocumentManager } from '../../documents';
Expand All @@ -14,7 +15,7 @@ import { TextDocument } from 'vscode-languageserver-textdocument';
import { offenseToDiagnostic } from '../../diagnostics/offenseToDiagnostic';

describe('Unit: ApplySuggestionProvider', () => {
const uri = URI.file('/path/to/file.liquid').toString();
const uri = path.normalize(URI.file('/path/to/file.liquid'));
const contents = `
{% assign x = 1 %}
<script src="2.js"></script>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,15 +1,16 @@
import { describe, it, expect, vi, beforeEach } from 'vitest';
import { URI } from 'vscode-uri';
import { path } from '@shopify/theme-check-common';
import { beforeEach, describe, expect, it, vi } from 'vitest';
import { Connection } from 'vscode-languageserver';
import { URI } from 'vscode-uri';
import { ClientCapabilities } from '../../ClientCapabilities';
import { DiagnosticsManager, makeRunChecks } from '../../diagnostics';
import { DocumentManager } from '../../documents';
import { DebouncedFunction } from '../../utils';
import { RunChecksProvider } from './RunChecksProvider';

describe('Unit: RunChecksProvider', () => {
const uri1 = URI.file('/path/to/file1.liquid').toString();
const uri2 = URI.file('/path/to/file2.liquid').toString();
const uri1 = path.normalize(URI.file('/path/to/file1.liquid'));
const uri2 = path.normalize(URI.file('/path/to/file2.liquid'));
const contents1 = `
{% assign x = 1 %}
<script src="2.js"></script>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { describe, beforeEach, it, expect } from 'vitest';
import { DocumentLinksProvider } from './DocumentLinksProvider';
import { path } from '@shopify/theme-check-common';
import { beforeEach, describe, expect, it } from 'vitest';
import { DocumentManager } from '../documents';
import { URI } from 'vscode-uri';
import { DocumentLinksProvider } from './DocumentLinksProvider';

describe('DocumentLinksProvider', () => {
let documentManager: DocumentManager;
Expand All @@ -16,7 +16,7 @@ describe('DocumentLinksProvider', () => {
const uriString = 'file:///path/to/non-liquid-html-document.txt';
const rootUri = 'file:///path/to/project';

documentManager.open(URI.parse(uriString).toString(), 'Sample plain text content', 1);
documentManager.open(path.normalize(uriString), 'Sample plain text content', 1);

const result = await documentLinksProvider.documentLinks(uriString, rootUri);
expect(result).toEqual([]);
Expand All @@ -43,7 +43,7 @@ describe('DocumentLinksProvider', () => {
{{ 'asset.js' | asset_url }}
`;

documentManager.open(URI.parse(uriString).toString(), liquidHtmlContent, 1);
documentManager.open(path.normalize(uriString), liquidHtmlContent, 1);

const result = await documentLinksProvider.documentLinks(uriString, rootUri);
const expectedUrls = [
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { expect, describe, it, beforeEach } from 'vitest';
import { DocumentManager } from './DocumentManager';
import { URI, Utils } from 'vscode-uri';
import { path } from '@shopify/theme-check-common';

describe('Module: DocumentManager', () => {
let documentManager: DocumentManager;
Expand All @@ -17,11 +18,11 @@ describe('Module: DocumentManager', () => {
// We expect forward slash paths (windows path get normalized)
expect(fileUri.path).not.to.include('\\');
documentManager.open(fileUri.toString(), '{{ "hi" }}', 0);
const theme = documentManager.theme(rootUri.toString());
const theme = documentManager.theme(path.normalize(rootUri));
expect(theme).to.have.lengthOf(1);
expect(theme[0].uri).not.to.include('\\');
// `fileURI.toString()` lowercases c: in 'C:\dir\path'
// Without the URI.parse().path, this test was failing for a dumb reason
expect(theme[0].uri).to.equal(fileUri.toString());
expect(theme[0].uri).to.equal(path.normalize(fileUri));
});
});
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { SourceCode, SourceCodeType, Theme, toSourceCode } from '@shopify/theme-check-common';
import { path, SourceCode, SourceCodeType, Theme, toSourceCode } from '@shopify/theme-check-common';
import { TextDocument } from 'vscode-languageserver-textdocument';
import { URI } from 'vscode-languageserver-types';

Expand Down Expand Up @@ -37,10 +37,11 @@ export class DocumentManager {
}

public get(uri: URI) {
return this.sourceCodes.get(uri);
return this.sourceCodes.get(path.normalize(uri));
}

private set(uri: URI, source: string, version: number | undefined) {
uri = path.normalize(uri);
// We only support json and liquid files.
if (!/\.(json|liquid)$/.test(uri) || /\.(s?css|js).liquid$/.test(uri)) {
return;
Expand Down
16 changes: 3 additions & 13 deletions packages/theme-language-server-node/src/dependencies.spec.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import * as mktemp from 'mktemp';
import * as fs from 'node:fs/promises';
import * as path from 'node:path';
import { path as pathUtils } from '@shopify/theme-check-common';
import { afterAll, beforeAll, describe, expect, it } from 'vitest';
import { URI, Utils } from 'vscode-uri';
import {
Expand Down Expand Up @@ -142,7 +143,7 @@ describe('Module: dependencies', () => {
it('should accurately return whether a file exists in workspace', async () => {
// Create a temporary file
const tempFilePath = path.join(workspace.root, 'temp.txt');
const tempFileUri = URI.file(tempFilePath).toString();
const tempFileUri = pathUtils.normalize(URI.file(tempFilePath));
await fs.writeFile(tempFilePath, 'Hello, world!', 'utf8');

// Use the fileExists function to check if the file exists
Expand Down Expand Up @@ -171,17 +172,6 @@ describe('Module: dependencies', () => {
expect(await getDefaultTranslations()).to.eql({ beverage: 'café' });
});
});

describe('Unit: loadConfig', () => {
it('should have a path normalized root', async () => {
expect((await loadConfig(workspace.uri('gitRootTheme/snippets'))).rootUri).not.to.include(
`\\`,
);
expect((await loadConfig(workspace.uri('frenchDefault/snippets'))).rootUri).not.to.include(
`\\`,
);
});
});
});

async function makeTempWorkspace(structure: Tree): Promise<Workspace> {
Expand All @@ -194,7 +184,7 @@ async function makeTempWorkspace(structure: Tree): Promise<Workspace> {
return {
root,
path: (relativePath) => path.join(root, ...relativePath.split('/')),
uri: (relativePath) => Utils.joinPath(rootUri, ...relativePath.split('/')).toString(),
uri: (relativePath) => pathUtils.normalize(Utils.joinPath(rootUri, ...relativePath.split('/'))),
clean: async () => fs.rm(root, { recursive: true, force: true }),
};

Expand Down
4 changes: 2 additions & 2 deletions packages/theme-language-server-node/src/dependencies.ts
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ export const loadConfig: Dependencies['loadConfig'] = async function loadConfig(
const configUri = Utils.joinPath(rootUri, '.theme-check.yml');
const configPath = asFsPath(configUri);
const [configExists, isDefinitelyThemeAppExtension] = await Promise.all([
fileExists(configUri.toString()),
fileExists(path.normalize(configUri)),
hasThemeAppExtensionConfig(rootUri.fsPath),
]);
if (configExists) {
Expand Down Expand Up @@ -129,7 +129,7 @@ function cached<T>(fn: (...args: any[]) => Promise<T>): (...args: any[]) => Prom
}

function normalizeRoot(config: Config) {
config.rootUri = URI.parse(config.rootUri).toString();
config.rootUri = path.normalize(config.rootUri);
return config;
}

Expand Down

0 comments on commit 35b07d1

Please sign in to comment.