diff --git a/core/autocomplete/prefiltering/index.ts b/core/autocomplete/prefiltering/index.ts index 40ffa5384..59905870e 100644 --- a/core/autocomplete/prefiltering/index.ts +++ b/core/autocomplete/prefiltering/index.ts @@ -3,9 +3,9 @@ import path from "node:path"; import ignore from "ignore"; import { IDE } from "../.."; -import { getBasename } from "../../util"; import { getConfigJsonPath } from "../../util/paths"; import { HelperVars } from "../util/HelperVars"; +import { getRelativePath } from "../../util/uri"; async function isDisabledForFile( currentFilepath: string, @@ -15,26 +15,11 @@ async function isDisabledForFile( if (disableInFiles) { // Relative path needed for `ignore` const workspaceDirs = await ide.getWorkspaceDirs(); - let filepath = currentFilepath; - for (const workspaceDir of workspaceDirs) { - const relativePath = path.relative(workspaceDir, filepath); - const relativePathBase = relativePath.split(path.sep).at(0); - const isInWorkspace = - !path.isAbsolute(relativePath) && relativePathBase !== ".."; - if (isInWorkspace) { - filepath = path.relative(workspaceDir, filepath); - break; - } - } - - // Worst case we can check filetype glob patterns - if (filepath === currentFilepath) { - filepath = getBasename(filepath); - } + const relativePath = getRelativePath(currentFilepath, workspaceDirs); // @ts-ignore const pattern = ignore.default().add(disableInFiles); - if (pattern.ignores(filepath)) { + if (pattern.ignores(relativePath)) { return true; } } diff --git a/core/index.d.ts b/core/index.d.ts index 9cd697321..d1f2b28fd 100644 --- a/core/index.d.ts +++ b/core/index.d.ts @@ -611,7 +611,7 @@ export interface IDE { getWorkspaceConfigs(): Promise; - fileExists(filepath: string): Promise; + fileExists(fileUri: string): Promise; writeFile(path: string, contents: string): Promise; @@ -625,20 +625,16 @@ export interface IDE { runCommand(command: string): Promise; - saveFile(filepath: string): Promise; + saveFile(fileUri: string): Promise; - readFile(filepath: string): Promise; + readFile(fileUri: string): Promise; - readRangeInFile(filepath: string, range: Range): Promise; + readRangeInFile(fileUri: string, range: Range): Promise; - showLines( - filepath: string, - startLine: number, - endLine: number, - ): Promise; + showLines(fileUri: string, startLine: number, endLine: number): Promise; showDiff( - filepath: string, + fileUri: string, newContents: string, stepIndex: number, ): Promise; @@ -660,7 +656,7 @@ export interface IDE { subprocess(command: string, cwd?: string): Promise<[string, string]>; - getProblems(filepath?: string | undefined): Promise; + getProblems(fileUri?: string | undefined): Promise; getBranch(dir: string): Promise; @@ -686,7 +682,7 @@ export interface IDE { gotoDefinition(location: Location): Promise; // Callbacks - onDidChangeActiveTextEditor(callback: (filepath: string) => void): void; + onDidChangeActiveTextEditor(callback: (fileUri: string) => void): void; } // Slash Commands diff --git a/core/protocol/messenger/messageIde.ts b/core/protocol/messenger/messageIde.ts index febdf559c..d19012d6e 100644 --- a/core/protocol/messenger/messageIde.ts +++ b/core/protocol/messenger/messageIde.ts @@ -27,16 +27,13 @@ export class MessageIde implements IDE { ) => void, ) {} - pathSep(): Promise { - return this.request("pathSep", undefined); - } - fileExists(filepath: string): Promise { - return this.request("fileExists", { filepath }); + fileExists(fileUri: string): Promise { + return this.request("fileExists", { filepath: fileUri }); } async gotoDefinition(location: Location): Promise { return this.request("gotoDefinition", { location }); } - onDidChangeActiveTextEditor(callback: (filepath: string) => void): void { + onDidChangeActiveTextEditor(callback: (fileUri: string) => void): void { this.on("didChangeActiveTextEditor", (data) => callback(data.filepath)); } @@ -126,11 +123,15 @@ export class MessageIde implements IDE { } async showLines( - filepath: string, + fileUri: string, startLine: number, endLine: number, ): Promise { - return await this.request("showLines", { filepath, startLine, endLine }); + return await this.request("showLines", { + filepath: fileUri, + startLine, + endLine, + }); } async listFolders(): Promise { @@ -148,16 +149,16 @@ export class MessageIde implements IDE { return dir; } - async writeFile(path: string, contents: string): Promise { - await this.request("writeFile", { path, contents }); + async writeFile(fileUri: string, contents: string): Promise { + await this.request("writeFile", { path: fileUri, contents }); } async showVirtualFile(title: string, contents: string): Promise { await this.request("showVirtualFile", { name: title, content: contents }); } - async openFile(path: string): Promise { - await this.request("openFile", { path }); + async openFile(fileUri: string): Promise { + await this.request("openFile", { path: fileUri }); } async openUrl(url: string): Promise { @@ -168,18 +169,22 @@ export class MessageIde implements IDE { await this.request("runCommand", { command }); } - async saveFile(filepath: string): Promise { - await this.request("saveFile", { filepath }); + async saveFile(fileUri: string): Promise { + await this.request("saveFile", { filepath: fileUri }); } - async readFile(filepath: string): Promise { - return await this.request("readFile", { filepath }); + async readFile(fileUri: string): Promise { + return await this.request("readFile", { filepath: fileUri }); } async showDiff( - filepath: string, + fileUri: string, newContents: string, stepIndex: number, ): Promise { - await this.request("showDiff", { filepath, newContents, stepIndex }); + await this.request("showDiff", { + filepath: fileUri, + newContents, + stepIndex, + }); } getOpenFiles(): Promise { @@ -198,8 +203,8 @@ export class MessageIde implements IDE { return this.request("getSearchResults", { query }); } - getProblems(filepath: string): Promise { - return this.request("getProblems", { filepath }); + getProblems(fileUri: string): Promise { + return this.request("getProblems", { filepath: fileUri }); } subprocess(command: string, cwd?: string): Promise<[string, string]> { diff --git a/core/test/testDir.ts b/core/test/testDir.ts index 7bc63105b..dcd410b75 100644 --- a/core/test/testDir.ts +++ b/core/test/testDir.ts @@ -1,20 +1,27 @@ import fs from "fs"; import os from "os"; import path from "path"; +import { + localPathOrUriToPath, + localPathToUri, + pathOrUriToUri, +} from "../util/uri"; +import { fileURLToPath } from "url"; // Want this outside of the git repository so we can change branches in tests -export const TEST_DIR = path.join(os.tmpdir(), "testWorkspaceDir"); +const TEST_DIR_PATH = path.join(os.tmpdir(), "testWorkspaceDir"); +export const TEST_DIR = localPathToUri(TEST_DIR_PATH); // URI export function setUpTestDir() { - if (fs.existsSync(TEST_DIR)) { - fs.rmSync(TEST_DIR, { recursive: true }); + if (fs.existsSync(TEST_DIR_PATH)) { + fs.rmSync(TEST_DIR_PATH, { recursive: true }); } - fs.mkdirSync(TEST_DIR); + fs.mkdirSync(TEST_DIR_PATH); } export function tearDownTestDir() { - if (fs.existsSync(TEST_DIR)) { - fs.rmSync(TEST_DIR, { recursive: true }); + if (fs.existsSync(TEST_DIR_PATH)) { + fs.rmSync(TEST_DIR_PATH, { recursive: true }); } } @@ -24,9 +31,18 @@ export function tearDownTestDir() { "index/index.ts" creates an empty index/index.ts ["index/index.ts", "hello"] creates index/index.ts with contents "hello" */ -export function addToTestDir(paths: (string | string[])[]) { +export function addToTestDir(pathsOrUris: (string | string[])[]) { + // Allow tests to use URIs or local paths + const paths = pathsOrUris.map((val) => { + if (Array.isArray(val)) { + return [localPathOrUriToPath(val[0]), val[1]]; + } else { + return localPathOrUriToPath(val); + } + }); + for (const p of paths) { - const filepath = path.join(TEST_DIR, Array.isArray(p) ? p[0] : p); + const filepath = path.join(TEST_DIR_PATH, Array.isArray(p) ? p[0] : p); fs.mkdirSync(path.dirname(filepath), { recursive: true }); if (Array.isArray(p)) { diff --git a/core/util/devdata.ts b/core/util/devdata.ts index 12723e915..3f786fa84 100644 --- a/core/util/devdata.ts +++ b/core/util/devdata.ts @@ -1,9 +1,9 @@ -import { writeFileSync } from "fs"; +import fs from "fs"; import { getDevDataFilePath } from "./paths.js"; export function logDevData(tableName: string, data: any) { const filepath: string = getDevDataFilePath(tableName); const jsonLine = JSON.stringify(data); - writeFileSync(filepath, `${jsonLine}\n`, { flag: "a" }); + fs.writeFileSync(filepath, `${jsonLine}\n`, { flag: "a" }); } diff --git a/core/util/filesystem.ts b/core/util/filesystem.ts index 39f048119..e11cf37ec 100644 --- a/core/util/filesystem.ts +++ b/core/util/filesystem.ts @@ -17,6 +17,7 @@ import { import { GetGhTokenArgs } from "../protocol/ide.js"; import { getContinueGlobalPath } from "./paths.js"; +import { fileURLToPath } from "node:url"; class FileSystemIde implements IDE { constructor(private readonly workspaceDir: string) {} @@ -27,14 +28,15 @@ class FileSystemIde implements IDE { ): Promise { return Promise.resolve(); } - fileExists(filepath: string): Promise { + fileExists(fileUri: string): Promise { + const filepath = fileURLToPath(fileUri); return Promise.resolve(fs.existsSync(filepath)); } gotoDefinition(location: Location): Promise { return Promise.resolve([]); } - onDidChangeActiveTextEditor(callback: (filepath: string) => void): void { + onDidChangeActiveTextEditor(callback: (fileUri: string) => void): void { return; } @@ -51,14 +53,17 @@ class FileSystemIde implements IDE { async getGitHubAuthToken(args: GetGhTokenArgs): Promise { return undefined; } - async getLastModified(files: string[]): Promise<{ [path: string]: number }> { + async getLastModified( + fileUris: string[], + ): Promise<{ [path: string]: number }> { const result: { [path: string]: number } = {}; - for (const file of files) { + for (const uri of fileUris) { try { - const stats = fs.statSync(file); - result[file] = stats.mtimeMs; + const filepath = fileURLToPath(uri); + const stats = fs.statSync(filepath); + result[uri] = stats.mtimeMs; } catch (error) { - console.error(`Error getting last modified time for ${file}:`, error); + console.error(`Error getting last modified time for ${uri}:`, error); } } return result; @@ -67,8 +72,9 @@ class FileSystemIde implements IDE { return Promise.resolve(dir); } async listDir(dir: string): Promise<[string, FileType][]> { + const filepath = fileURLToPath(dir); const all: [string, FileType][] = fs - .readdirSync(dir, { withFileTypes: true }) + .readdirSync(filepath, { withFileTypes: true }) .map((dirent: any) => [ dirent.name, dirent.isDirectory() @@ -105,7 +111,7 @@ class FileSystemIde implements IDE { }); } - readRangeInFile(filepath: string, range: Range): Promise { + readRangeInFile(fileUri: string, range: Range): Promise { return Promise.resolve(""); } @@ -149,7 +155,7 @@ class FileSystemIde implements IDE { } showLines( - filepath: string, + fileUri: string, startLine: number, endLine: number, ): Promise { @@ -164,9 +170,10 @@ class FileSystemIde implements IDE { return Promise.resolve([]); } - writeFile(path: string, contents: string): Promise { + writeFile(fileUri: string, contents: string): Promise { + const filepath = fileURLToPath(fileUri); return new Promise((resolve, reject) => { - fs.writeFile(path, contents, (err) => { + fs.writeFile(filepath, contents, (err) => { if (err) { reject(err); } @@ -195,11 +202,12 @@ class FileSystemIde implements IDE { return Promise.resolve(); } - saveFile(filepath: string): Promise { + saveFile(fileUri: string): Promise { return Promise.resolve(); } - readFile(filepath: string): Promise { + readFile(fileUri: string): Promise { + const filepath = fileURLToPath(fileUri); return new Promise((resolve, reject) => { fs.readFile(filepath, "utf8", (err, contents) => { if (err) { @@ -215,7 +223,7 @@ class FileSystemIde implements IDE { } showDiff( - filepath: string, + fileUri: string, newContents: string, stepIndex: number, ): Promise { @@ -238,7 +246,7 @@ class FileSystemIde implements IDE { return ""; } - async getProblems(filepath?: string | undefined): Promise { + async getProblems(fileUri?: string | undefined): Promise { return Promise.resolve([]); } diff --git a/core/util/uri.ts b/core/util/uri.ts index 1a0011582..f7e4b9fb4 100644 --- a/core/util/uri.ts +++ b/core/util/uri.ts @@ -1,4 +1,5 @@ import URI from "uri-js"; +import { fileURLToPath, pathToFileURL } from "url"; export function getFullPath(uri: string): string { try { @@ -9,6 +10,11 @@ export function getFullPath(uri: string): string { } } +export function localPathToUri(path: string) { + const url = pathToFileURL(path); + return URI.normalize(url.toString()); +} + export function pathToUriPathSegment(path: string) { // Converts any OS path to cleaned up URI path segment format with no leading/trailing slashes // e.g. \path\to\folder\ -> path/to/folder @@ -30,6 +36,37 @@ export function getRelativePath( const fullPath = getFullPath(fileUri); } +/* + +*/ +export function localPathOrUriToPath(localPathOrUri: string): string { + try { + return fileURLToPath(localPathOrUri); + } catch (e) { + console.log("Converted url to path"); + return localPathOrUri; + } +} + +/* + To smooth out the transition from path to URI will use this function to warn when path is used +*/ +export function pathOrUriToUri( + pathOrUri: string, + workspaceDirUris: string[], + showTraceOnPath = true, +): string { + try { + // URI.parse(pathOrUri); + + return pathOrUri; + } catch (e) { + if (showTraceOnPath) { + console.trace("Received relative path", e); + } + } +} + export function splitUriPath(uri: string): string[] { let parts = path.includes("/") ? path.split("/") : path.split("\\"); if (withRoot !== undefined) { diff --git a/extensions/vscode/src/VsCodeIde.ts b/extensions/vscode/src/VsCodeIde.ts index 5a0c988e1..2f3a57672 100644 --- a/extensions/vscode/src/VsCodeIde.ts +++ b/extensions/vscode/src/VsCodeIde.ts @@ -265,9 +265,9 @@ class VsCodeIde implements IDE { }); } - readRangeInFile(filepath: string, range: Range): Promise { + readRangeInFile(fileUri: string, range: Range): Promise { return this.ideUtils.readRangeInFile( - vscode.Uri.parse(filepath), + vscode.Uri.parse(fileUri), new vscode.Range( new vscode.Position(range.start.line, range.start.character), new vscode.Position(range.end.line, range.end.character), @@ -378,9 +378,9 @@ class VsCodeIde implements IDE { return getContinueGlobalPath(); } - async writeFile(path: string, contents: string): Promise { + async writeFile(fileUri: string, contents: string): Promise { await vscode.workspace.fs.writeFile( - vscode.Uri.parse(path), + vscode.Uri.parse(fileUri), Buffer.from(contents), ); } @@ -389,12 +389,12 @@ class VsCodeIde implements IDE { this.ideUtils.showVirtualFile(title, contents); } - async openFile(path: string): Promise { - await this.ideUtils.openFile(vscode.Uri.parse(path)); + async openFile(fileUri: string): Promise { + await this.ideUtils.openFile(vscode.Uri.parse(fileUri)); } async showLines( - filepath: string, + fileUri: string, startLine: number, endLine: number, ): Promise { @@ -402,7 +402,7 @@ class VsCodeIde implements IDE { new vscode.Position(startLine, 0), new vscode.Position(endLine, 0), ); - openEditorAndRevealRange(vscode.Uri.parse(filepath), range).then( + openEditorAndRevealRange(vscode.Uri.parse(fileUri), range).then( (editor) => { // Select the lines editor.selection = new vscode.Selection( @@ -426,8 +426,8 @@ class VsCodeIde implements IDE { } } - async saveFile(filepath: string): Promise { - await this.ideUtils.saveFile(vscode.Uri.parse(filepath)); + async saveFile(fileUri: string): Promise { + await this.ideUtils.saveFile(vscode.Uri.parse(fileUri)); } private static MAX_BYTES = 100000; @@ -482,11 +482,11 @@ class VsCodeIde implements IDE { } async showDiff( - filepath: string, + fileUri: string, newContents: string, stepIndex: number, ): Promise { - await this.diffManager.writeDiff(filepath, newContents, stepIndex); + await this.diffManager.writeDiff(fileUri, newContents, stepIndex); } async getOpenFiles(): Promise { @@ -563,9 +563,9 @@ class VsCodeIde implements IDE { return results.join("\n\n"); } - async getProblems(filepath?: string | undefined): Promise { - const uri = filepath - ? vscode.Uri.file(filepath) + async getProblems(fileUri?: string | undefined): Promise { + const uri = fileUri + ? vscode.Uri.parse(fileUri) : vscode.window.activeTextEditor?.document.uri; if (!uri) { return [];