8000 feat: handle when starting debug session failed by kittaakos · Pull Request #1809 · arduino/arduino-ide · GitHub
[go: up one dir, main page]

Skip to content

feat: handle when starting debug session failed #1809

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

Merged
merged 2 commits into from
Jan 23, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
fix: provide multiple build path guesses
IDE2 does not know what casing the CLI uses

Signed-off-by: Akos Kitta <a.kitta@arduino.cc>
  • Loading branch information
Akos Kitta committed Jan 19, 2023
commit 9228dfb83a88abe178d9274c33a5c337fab8989f
6 changes: 4 additions & 2 deletions arduino-ide-extension/src/browser/contributions/debug.ts
Original file line number Diff line number Diff line change
Expand Up @@ -246,8 +246,10 @@ export class Debug extends SketchContribution {
): Promise<boolean> {
if (err instanceof Error) {
try {
const tempBuildPath = await this.sketchService.tempBuildPath(sketch);
return err.message.includes(tempBuildPath);
const tempBuildPaths = await this.sketchService.tempBuildPath(sketch);
return tempBuildPaths.some((tempBuildPath) =>
err.message.includes(tempBuildPath)
);
} catch {
return false;
}
Expand Down
9 changes: 7 additions & 2 deletions arduino-ide-extension/src/common/protocol/sketches-service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -108,9 +108,14 @@ export interface SketchesService {

/**
* This is the JS/TS re-implementation of [`GenBuildPath`](https://github.com/arduino/arduino-cli/blob/c0d4e4407d80aabad81142693513b3306759cfa6/arduino/sketch/sketch.go#L296-L306) of the CLI.
* Pass in a sketch and get the build temporary folder filesystem path calculated from the main sketch file location. This method does not check the existence of the sketch.
* Pass in a sketch and get the build temporary folder filesystem path calculated from the main sketch file location. Can be multiple ones. This method does not check the existence of the sketch.
*
* The case sensitivity of the drive letter on Windows matters when the CLI calculates the MD5 hash of the temporary build folder.
* IDE2 does not know and does not want to rely on how the CLI treats the paths: with lowercase or uppercase drive letters.
* Hence, IDE2 has to provide multiple build paths on Windows. This hack will be obsolete when the CLI can provide error codes:
* https://github.com/arduino/arduino-cli/issues/1762.
*/
tempBuildPath(sketch: Sketch): Promise<string>;
tempBuildPath(sketch: Sketch): Promise<string[]>;
}

export interface SketchRef {
Expand Down
6 changes: 5 additions & 1 deletion arduino-ide-extension/src/common/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,10 @@ export function firstToUpperCase(what: string): string {
return what.charAt(0).toUpperCase() + what.slice(1);
}

export function isNullOrUndefined(what: any): what is undefined | null {
export function startsWithUpperCase(what: string): boolean {
return !!what && what.charAt(0) === firstToUpperCase(what.charAt(0));
}

export function isNullOrUndefined(what: unknown): what is undefined | null {
return what === undefined || what === null;
}
2 changes: 1 addition & 1 deletion arduino-ide-extension/src/node/is-temp-sketch.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import { isWindows, isOSX } from '@theia/core/lib/common/os';
import { injectable } from '@theia/core/shared/inversify';
import { firstToLowerCase } from '../common/utils';

const Win32DriveRegex = /^[a-zA-Z]:\\/;
export const Win32DriveRegex = /^[a-zA-Z]:\\/;
export const TempSketchPrefix = '.arduinoIDE-unsaved';

@injectable()
Expand Down
48 changes: 45 additions & 3 deletions arduino-ide-extension/src/node/sketches-service-impl.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,9 +33,16 @@ import {
IsTempSketch,
maybeNormalizeDrive,
TempSketchPrefix,
Win32DriveRegex,
} from './is-temp-sketch';
import { join } from 'path';
import { ErrnoException } from './utils/errors';
import { isWindows } from '@theia/core/lib/common/os';
import {
firstToLowerCase,
firstToUpperCase,
startsWithUpperCase,
} from '../common/utils';

const RecentSketches = 'recent-sketches.json';
const DefaultIno = `void setup() {
Expand Down Expand Up @@ -576,14 +583,49 @@ export class SketchesServiceImpl
);
}

async tempBuildPath(sketch: Sketch): Promise<string> {
async tempBuildPath(sketch: Sketch): Promise<string[]> {
const sketchPath = FileUri.fsPath(sketch.uri);
const { tempDirRealpath } = this.isTempSketch;
const tempBuildPaths = [
this.tempBuildPathMD5Hash(tempDirRealpath, sketchPath),
];

// If on Windows, provide both the upper and the lowercase drive letter MD5 hashes. All together four paths are expected:
// One of them should match if the sketch is not yet compiled.
// https://github.com/arduino/arduino-ide/pull/1809#discussion_r1071031040
if (isWindows && Win32DriveRegex.test(tempDirRealpath)) {
const toggleFirstCharCasing = (s: string) =>
startsWithUpperCase(s) ? firstToLowerCase(s) : firstToUpperCase(s);
const otherCaseTempDirRealPath = toggleFirstCharCasing(tempDirRealpath);
tempBuildPaths.push(
this.tempBuildPathMD5Hash(otherCaseTempDirRealPath, sketchPath)
);
if (Win32DriveRegex.test(sketchPath)) {
const otherCaseSketchPath = toggleFirstCharCasing(sketchPath);
tempBuildPaths.push(
this.tempBuildPathMD5Hash(tempDirRealpath, otherCaseSketchPath),
this.tempBuildPathMD5Hash(
otherCaseTempDirRealPath,
otherCaseSketchPath
)
);
}
}
return tempBuildPaths;
}

private tempBuildPathMD5Hash(tempFolderPath: string, path: string): string {
return join(tempFolderPath, this.tempBuildFolderMD5Hash(path));
}

private tempBuildFolderMD5Hash(path: string): string {
const hash = crypto
.createHash('md5')
.update(sketchPath)
.update(path)
.digest('hex')
.toUpperCase();
return join(this.isTempSketch.tempDirRealpath, `arduino-sketch-${hash}`);
const folderName = `arduino-sketch-${hash}`;
Copy link
Contributor

Choose a reason for hiding this comment

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

This is correct for the version of Arduino CLI currently used by Arduino IDE, but it will break when the Arduino CLI dependency is bumped:

After those changes, the path is now <temp dir>/arduino/sketches/<hash>.

No action needed now, but I thought to add a note in anticipation of the adjustment that will be needed at the time of the Arduino CLI dependency bump.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Thank you for the heads-up!

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I pushed a test for the build path: fd38ba4.

I had to change the IDE2 logic when stopping and restarting the daemon. I have to double-check it.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I have to double-check it.

The ready deferred promise is not used anywhere else; the change must not break anything. We need to clean up this code next time we change the core gRPC clients.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I have decided to drop everything I changed after the review and moved it to a separate PR: #1823. If the build is green, I would like to merge it as it is if you are OK with it, Per. Thanks!

Copy link
Contributor

Choose a reason for hiding this comment

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

Yes, I'm OK with that. Feel free to merge anytime you are ready Akos.

return folderName;
}

async deleteSketch(sketch: Sketch): Promise<void> {
Expand Down
0