Skip to content

Commit 45446b2

Browse files
author
Akos Kitta
committed
fix: invalid sketch name error detection on win32
Signed-off-by: Akos Kitta <[email protected]>
1 parent fc4a668 commit 45446b2

File tree

4 files changed

+44
-18
lines changed

4 files changed

+44
-18
lines changed

Diff for: arduino-ide-extension/package.json

+1
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,7 @@
4545
"@types/deepmerge": "^2.2.0",
4646
"@types/glob": "^7.2.0",
4747
"@types/google-protobuf": "^3.7.2",
48+
"@types/is-valid-path": "^0.1.0",
4849
"@types/js-yaml": "^3.12.2",
4950
"@types/keytar": "^4.4.0",
5051
"@types/lodash.debounce": "^4.0.6",

Diff for: arduino-ide-extension/src/electron-main/theia/electron-main-application.ts

+31-8
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ import {
2727
CLOSE_PLOTTER_WINDOW,
2828
SHOW_PLOTTER_WINDOW,
2929
} from '../../common/ipc-communication';
30+
import isValidPath = require('is-valid-path');
3031

3132
app.commandLine.appendSwitch('disable-http-cache');
3233

@@ -143,10 +144,12 @@ export class ElectronMainApplication extends TheiaElectronMainApplication {
143144
app.on('open-file', async (event, path) => {
144145
event.preventDefault();
145146
const resolvedPath = await this.resolvePath(path, cwd);
146-
const sketchFolderPath = await this.isValidSketchPath(resolvedPath);
147-
if (sketchFolderPath) {
148-
this.openFilePromise.reject(new InterruptWorkspaceRestoreError());
149-
await this.openSketch(sketchFolderPath);
147+
if (resolvedPath) {
148+
const sketchFolderPath = await this.isValidSketchPath(resolvedPath);
149+
if (sketchFolderPath) {
150+
this.openFilePromise.reject(new InterruptWorkspaceRestoreError());
151+
await this.openSketch(sketchFolderPath);
152+
}
150153
}
151154
});
152155
setTimeout(() => this.openFilePromise.resolve(), 500);
@@ -198,11 +201,25 @@ export class ElectronMainApplication extends TheiaElectronMainApplication {
198201
}
199202
}
200203

201-
private async resolvePath(path: string, cwd: string): Promise<string> {
202-
if (isAbsolute(path)) {
203-
return path;
204+
private async resolvePath(
205+
maybePath: string,
206+
cwd: string
207+
): Promise<string | undefined> {
208+
if (!isValidPath(maybePath)) {
209+
return undefined;
210+
}
211+
if (isAbsolute(maybePath)) {
212+
return maybePath;
213+
}
214+
try {
215+
const resolved = await fs.realpath(resolve(cwd, maybePath));
216+
return resolved;
217+
} catch (err) {
218+
if ('code' in err && err.code === 'ENOENT') {
219+
return undefined;
220+
}
221+
throw err;
204222
}
205-
return fs.realpath(resolve(cwd, path));
206223
}
207224

208225
protected override async launch(
@@ -236,6 +253,9 @@ export class ElectronMainApplication extends TheiaElectronMainApplication {
236253
);
237254
for (const workspace of workspaces) {
238255
const resolvedPath = await this.resolvePath(workspace.file, params.cwd);
256+
if (!resolvedPath) {
257+
continue;
258+
}
239259
const sketchFolderPath = await this.isValidSketchPath(resolvedPath);
240260
if (sketchFolderPath) {
241261
workspace.file = sketchFolderPath;
@@ -264,6 +284,9 @@ export class ElectronMainApplication extends TheiaElectronMainApplication {
264284
let path: string | undefined;
265285
for (const maybePath of argCopy) {
266286
const resolvedPath = await this.resolvePath(maybePath, params.cwd);
287+
if (!resolvedPath) {
288+
continue;
289+
}
267290
const sketchFolderPath = await this.isValidSketchPath(resolvedPath);
268291
if (sketchFolderPath) {
269292
path = sketchFolderPath;

Diff for: arduino-ide-extension/src/node/sketches-service-impl.ts

+7-10
Original file line numberDiff line numberDiff line change
@@ -668,22 +668,19 @@ function isNotFoundError(err: unknown): err is ServiceError {
668668
* Nothing guarantees that the invalid existing main sketch file still exits by the time client performs the sketch move.
669669
*/
670670
async function isInvalidSketchNameError(
671-
err: unknown,
671+
cliErr: unknown,
672672
requestSketchPath: string
673673
): Promise<string | undefined> {
674-
if (isNotFoundError(err)) {
674+
if (isNotFoundError(cliErr)) {
675675
const ino = requestSketchPath.endsWith('.ino');
676676
if (ino) {
677677
const sketchFolderPath = path.dirname(requestSketchPath);
678678
const sketchName = path.basename(sketchFolderPath);
679-
if (
680-
new RegExp(
681-
`${invalidSketchNameErrorRegExpPrefix}${path.join(
682-
sketchFolderPath,
683-
`${sketchName}.ino`
684-
)}`
685-
).test(err.details)
686-
) {
679+
const pattern = `${invalidSketchNameErrorRegExpPrefix}${path.join(
680+
sketchFolderPath,
681+
`${sketchName}.ino`
682+
)}`.replace(/\\/g, '\\\\'); // make windows path separator with \\ to have a valid regexp.
683+
if (new RegExp(pattern, 'i').test(cliErr.details)) {
687684
try {
688685
await fs.access(requestSketchPath);
689686
return requestSketchPath;

Diff for: yarn.lock

+5
Original file line numberDiff line numberDiff line change
@@ -3139,6 +3139,11 @@
31393139
resolved "https://registry.yarnpkg.com/@types/http-cache-semantics/-/http-cache-semantics-4.0.1.tgz#0ea7b61496902b95890dc4c3a116b60cb8dae812"
31403140
integrity sha512-SZs7ekbP8CN0txVG2xVRH6EgKmEm31BOxA07vkFaETzZz1xh+cbt8BcI0slpymvwhx5dlFnQG2rTlPVQn+iRPQ==
31413141

3142+
"@types/is-valid-path@^0.1.0":
3143+
version "0.1.0"
3144+
resolved "https://registry.yarnpkg.com/@types/is-valid-path/-/is-valid-path-0.1.0.tgz#d5c6e96801303112c9626d44268c6fabc72d272f"
3145+
integrity sha512-2ontWtpN8O2nf5S7EjDDJ0DwrRa2t7wmS3Wmo322yWYG6yFBYC1QCaLhz4Iz+mzJy8Kf4zP5yVyEd1ANPDmOFQ==
3146+
31423147
"@types/js-yaml@^3.12.2":
31433148
version "3.12.7"
31443149
resolved "https://registry.yarnpkg.com/@types/js-yaml/-/js-yaml-3.12.7.tgz#330c5d97a3500e9c903210d6e49f02964af04a0e"

0 commit comments

Comments
 (0)