Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: tweaking relative path handling for windows compatibility #18

Merged
merged 11 commits into from
Dec 14, 2023
1 change: 1 addition & 0 deletions .gitattributes
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
* text=auto eol=lf
3 changes: 2 additions & 1 deletion .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,11 @@ on:
pull_request:
jobs:
test:
runs-on: ubuntu-latest
runs-on: ${{ matrix.os }}
strategy:
fail-fast: false
matrix:
os: [ubuntu-latest, windows-latest]
aorumbayev marked this conversation as resolved.
Show resolved Hide resolved
node-version: ['18']
steps:
- uses: actions/checkout@v4
Expand Down
3 changes: 2 additions & 1 deletion .prettierrc.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
{
"semi": true,
"trailingComma": "all",
"singleQuote": true
"singleQuote": true,
"endOfLine": "lf"
}
5 changes: 4 additions & 1 deletion .vscode/settings.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,5 +13,8 @@
"git.branchProtectionPrompt": "alwaysCommitToNewBranch",

"editor.defaultFormatter": "esbenp.prettier-vscode",
"editor.formatOnSave": true
"editor.formatOnSave": true,

"mochaExplorer.files": "tests/**/*.ts",
"mochaExplorer.require": "ts-node/register"
}
38 changes: 27 additions & 11 deletions extension/src/fileAccessor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,30 +4,46 @@ import { FileAccessor } from '../../src/common';
export const workspaceFileAccessor: FileAccessor = {
isWindows: typeof process !== 'undefined' && process.platform === 'win32',
readFile(path: string): Promise<Uint8Array> {
const uri = pathToUri(path);
const uri = vscode.Uri.file(path);
return thenableToPromise(vscode.workspace.fs.readFile(uri));
},
writeFile(path: string, contents: Uint8Array): Promise<void> {
const uri = pathToUri(path);
const uri = vscode.Uri.file(path);
return thenableToPromise(vscode.workspace.fs.writeFile(uri, contents));
},
basename(path: string): string {
const uri = pathToUri(path);
const uri = vscode.Uri.file(path);
const lastSlash = uri.path.lastIndexOf('/');
if (lastSlash === -1) {
return path;
}
return uri.path.substring(lastSlash + 1);
},
};
filePathRelativeTo(base: string, filePath: string): string {
// Check if filePath is an absolute path
if (this.isWindows) {
if (filePath.match(/^[a-zA-Z]:\\/)) {
return filePath;
}
} else {
if (filePath.startsWith('/')) {
return filePath;
}
}

function pathToUri(path: string) {
try {
return vscode.Uri.file(path);
} catch (e) {
return vscode.Uri.parse(path, true);
}
}
// Create a Uri object with the base path
let baseUri = vscode.Uri.file(base);
if (!baseUri.path.endsWith('/')) {
// If the base path is not a directory, get its parent directory
baseUri = vscode.Uri.joinPath(baseUri, '..');
}

// Resolve the file path against the base Uri
const fullUri = vscode.Uri.joinPath(baseUri, filePath);

return fullUri.fsPath;
},
};

function thenableToPromise<T>(t: Thenable<T>): Promise<T> {
return new Promise((resolve, reject) => {
Expand Down
128 changes: 122 additions & 6 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 3 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@
"src"
],
"scripts": {
"compile": "rimraf out && tsc -p ./ && cp -R algosdk out/algosdk",
"compile": "shx rm -rf out && tsc -p ./ && shx cp -R algosdk out/algosdk",
"lint": "eslint src --ext ts",
"typecheck": "tsc -p tsconfig.json --noEmit",
"check-format": "prettier . --check",
Expand All @@ -63,7 +63,7 @@
"extension:package": "vsce package",
"extension:publish": "vsce publish",
"extension:publish-pre-release": "vsce publish --pre-release",
"test": "ts-mocha -p tsconfig.json tests/*test.ts --timeout 30s --diff false",
"test": "ts-mocha -p tsconfig.json 'tests/**/*test.ts' --timeout 30s --diff false",
"test:coverage": "nyc npm run test"
},
"dependencies": {
Expand Down Expand Up @@ -97,7 +97,7 @@
"mocha": "^10.2.0",
"nyc": "^15.1.0",
"prettier": "^3.0.3",
"rimraf": "^3.0.2",
"shx": "^0.3.4",
aorumbayev marked this conversation as resolved.
Show resolved Hide resolved
"ts-mocha": "^10.0.0",
"typescript": "^4.6.3",
"url": "^0.11.3"
Expand Down
1 change: 1 addition & 0 deletions src/common/fileAccessor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,5 +2,6 @@ export interface FileAccessor {
isWindows: boolean;
readFile(path: string): Promise<Uint8Array>;
writeFile(path: string, contents: Uint8Array): Promise<void>;
filePathRelativeTo(base: string, filePath: string): string;
basename(path: string): string;
}
20 changes: 8 additions & 12 deletions src/common/runtime.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,11 @@ import {
TraceReplayStackFrame,
} from './traceReplayEngine';
import { FileAccessor } from './fileAccessor';
import { AvmDebuggingAssets, ProgramSourceDescriptor } from './utils';
import {
AvmDebuggingAssets,
ProgramSourceDescriptor,
normalizePathAndCasing,
} from './utils';

export interface IRuntimeBreakpoint {
id: number;
Expand Down Expand Up @@ -249,7 +253,7 @@ export class AvmRuntime extends EventEmitter {
line: number,
column?: number,
): IRuntimeBreakpoint {
path = this.normalizePathAndCasing(path);
path = normalizePathAndCasing(this.fileAccessor, path);

const bp: IRuntimeBreakpoint = {
verified: false,
Expand All @@ -269,7 +273,7 @@ export class AvmRuntime extends EventEmitter {
}

public clearBreakpoints(path: string): void {
this.breakPoints.delete(this.normalizePathAndCasing(path));
this.breakPoints.delete(normalizePathAndCasing(this.fileAccessor, path));
}

public getAppStateReferences(): number[] {
Expand Down Expand Up @@ -359,14 +363,6 @@ export class AvmRuntime extends EventEmitter {
}, 0);
}

private normalizePathAndCasing(filePath: string) {
if (this.fileAccessor.isWindows) {
return filePath.replace(/\//g, '\\').toLowerCase();
} else {
return filePath.replace(/\\/g, '/');
}
}

private isFrameLocationOnBreakpoint(
location: FrameSourceLocation,
bp: IRuntimeBreakpointLocation,
Expand All @@ -386,7 +382,7 @@ export class AvmRuntime extends EventEmitter {
private findSourceDescriptorsForPath(
filePath: string,
): Array<{ descriptor: ProgramSourceDescriptor; sourceIndex: number }> {
filePath = this.normalizePathAndCasing(filePath);
filePath = normalizePathAndCasing(this.fileAccessor, filePath);

const sourceDescriptors: Array<{
descriptor: ProgramSourceDescriptor;
Expand Down
Loading
Loading