Skip to content

Correct XDG_CURRENT_DESKTOP env var - #1754

Merged
esimkowitz merged 6 commits into
mainfrom
evan/x-www-browser
Jan 16, 2025
Merged

esimkowitz merged 6 commits into
mainfrom
evan/x-www-browser

Conversation

@esimkowitz

@esimkowitz esimkowitz commented Jan 16, 2025 •

Copy link
Copy Markdown
Contributor

Electron sets XDG_CURRENT_DESKTOP for better Chromium compatibility on Linux. We need to unset this for wavesrv and for any calls to open a file or URL externally in emain.

See https://www.electronjs.org/docs/latest/api/environment-variables#original_xdg_current_desktop

There's a bug open in Electron related to this: electron/electron#45129

closes #1733

@coderabbitai

coderabbitai Bot commented Jan 16, 2025 •

Copy link
Copy Markdown
Contributor

Warning

Rate limit exceeded

@esimkowitz has exceeded the limit for the number of commits or files that can be reviewed per hour. Please wait 1 minutes and 9 seconds before requesting another review.

⌛ How to resolve this issue?

After the wait time has elapsed, a review can be triggered using the @coderabbitai review command as a PR comment. Alternatively, push new commits to this PR.

We recommend that you space out your commits to avoid hitting the rate limit.

🚦 How do rate limits work?

CodeRabbit enforces hourly rate limits for each developer per organization.

Our paid plans have higher rate limits than the trial, open-source and free plans. In all cases, we re-allow further reviews after a brief timeout.

Please see our FAQ for further information.

📥 Commits

Reviewing files that changed from the base of the PR and between c19df0a and 0e2409b.

📒 Files selected for processing (1)
  • emain/platform.ts (1 hunks)

Walkthrough

The pull request introduces a new function getXdgCurrentDesktop() in the platform.ts module, which retrieves the desktop environment information by checking environment variables. This function is subsequently imported and used in multiple files (emain-wavesrv.ts and emain.ts) to manage and preserve the XDG_CURRENT_DESKTOP environment variable during various system interactions.

In emain-wavesrv.ts, the function is used to set the desktop environment variable when spawning a process. In emain.ts, it is employed to manage the environment variable during external URL and file path opening operations, ensuring that the original desktop environment context is maintained before and after these actions. The implementation provides a consistent approach to handling desktop environment information across different parts of the application.


Thank you for using CodeRabbit. We offer it for free to the OSS community and would appreciate your support in helping us grow. If you find it useful, would you consider giving us a shout-out on your favorite social media?

❤️ Share
🪧 Tips

Chat

There are 3 ways to chat with CodeRabbit:

  • Review comments: Directly reply to a review comment made by CodeRabbit. Example:
    • I pushed a fix in commit <commit_id>, please review it.
    • Generate unit testing code for this file.
    • Open a follow-up GitHub issue for this discussion.
  • Files and specific lines of code (under the "Files changed" tab): Tag @coderabbitai in a new review comment at the desired location with your query. Examples:
    • @coderabbitai generate unit testing code for this file.
    • @coderabbitai modularize this function.
  • PR comments: Tag @coderabbitai in a new PR comment to ask questions about the PR branch. For the best results, please provide a very specific query, as very limited context is provided in this mode. Examples:
    • @coderabbitai gather interesting stats about this repository and render them as a table. Additionally, render a pie chart showing the language distribution in the codebase.
    • @coderabbitai read src/utils.ts and generate unit testing code.
    • @coderabbitai read the files in the src/scheduler package and generate a class diagram using mermaid and a README in the markdown format.
    • @coderabbitai help me debug CodeRabbit configuration file.

Note: Be mindful of the bot's finite context window. It's strongly recommended to break down tasks such as reading entire modules into smaller chunks. For a focused discussion, use review comments to chat about specific files and their changes, instead of using the PR comments.

CodeRabbit Commands (Invoked using PR comments)

  • @coderabbitai pause to pause the reviews on a PR.
  • @coderabbitai resume to resume the paused reviews.
  • @coderabbitai review to trigger an incremental review. This is useful when automatic reviews are disabled for the repository.
  • @coderabbitai full review to do a full review from scratch and review all the files again.
  • @coderabbitai summary to regenerate the summary of the PR.
  • @coderabbitai generate docstrings to generate docstrings for this PR. (Beta)
  • @coderabbitai resolve resolve all the CodeRabbit review comments.
  • @coderabbitai configuration to show the current CodeRabbit configuration for the repository.
  • @coderabbitai help to get help.

Other keywords and placeholders

  • Add @coderabbitai ignore anywhere in the PR description to prevent this PR from being reviewed.
  • Add @coderabbitai summary to generate the high-level summary at a specific location in the PR description.
  • Add @coderabbitai anywhere in the PR title to generate the title automatically.

CodeRabbit Configuration File (.coderabbit.yaml)

  • You can programmatically configure CodeRabbit by adding a .coderabbit.yaml file to the root of your repository.
  • Please see the configuration documentation for more information.
  • If your editor has YAML language server enabled, you can add the path at the top of this file to enable auto-completion and validation: # yaml-language-server: $schema=https://coderabbit.ai/integrations/schema.v2.json

Documentation and Community

  • Visit our Documentation for detailed information on how to use CodeRabbit.
  • Join our Discord Community to get help, request features, and share feedback.
  • Follow us on X/Twitter for updates and announcements.

@esimkowitz esimkowitz changed the title Try opening external URLs with x-www-browser first for Linux Correct XDG_CURRENT_DESKTOP env var for Snap Jan 16, 2025
@esimkowitz esimkowitz changed the title Correct XDG_CURRENT_DESKTOP env var for Snap Correct XDG_CURRENT_DESKTOP env var Jan 16, 2025
@esimkowitz
esimkowitz marked this pull request as ready for review January 16, 2025 10:12

@coderabbitai coderabbitai Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Actionable comments posted: 1

🧹 Nitpick comments (1)
emain/emain.ts (1)

125-135: Consider adding error handling for environment variable restoration.

The implementation correctly manages the desktop environment during external URL opening. However, consider using try-finally to ensure environment restoration even if an error occurs.

 fireAndForget(async () => {
     const curXdgCurrentDesktop = process.env.XDG_CURRENT_DESKTOP;
     if (curXdgCurrentDesktop) {
         process.env.XDG_CURRENT_DESKTOP = getXdgCurrentDesktop();
     }
-    await electron.shell.openExternal(url).catch((err) => {
-        console.error(`Failed to open URL ${url}:`, err);
-    });
-    if (curXdgCurrentDesktop) {
-        process.env.XDG_CURRENT_DESKTOP = curXdgCurrentDesktop;
-    }
+    try {
+        await electron.shell.openExternal(url);
+    } catch (err) {
+        console.error(`Failed to open URL ${url}:`, err);
+    } finally {
+        if (curXdgCurrentDesktop) {
+            process.env.XDG_CURRENT_DESKTOP = curXdgCurrentDesktop;
+        }
+    }
 });
📜 Review details

Configuration used: CodeRabbit UI
Review profile: CHILL
Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between 3beb14a and e2d69c1.

📒 Files selected for processing (3)
  • emain/emain-wavesrv.ts (2 hunks)
  • emain/emain.ts (3 hunks)
  • emain/platform.ts (1 hunks)
⏰ Context from checks skipped due to timeout of 90000ms (3)
  • GitHub Check: Analyze (javascript-typescript)
  • GitHub Check: Analyze (go)
  • GitHub Check: Build for TestDriver.ai
🔇 Additional comments (4)
emain/emain-wavesrv.ts (1)

57-60: LGTM! Environment properly configured for wavesrv.

The implementation correctly preserves the desktop environment context when launching wavesrv.

emain/emain.ts (1)

359-370: Apply the same error handling improvement as suggested for open-external.

The implementation correctly manages the desktop environment during native path opening.

emain/platform.ts (2)

212-212: LGTM!

The export is correctly positioned and follows the module's export pattern.


197-203: Verify integration with dependent modules.

Since this function is used by emain-wavesrv.ts and emain.ts for managing XDG_CURRENT_DESKTOP, let's verify the integration points:

✅ Verification successful

Integration verified successfully

The function is properly exported and correctly used across the codebase. The state management in emain.ts follows a consistent pattern of saving, modifying, and restoring the desktop environment value.

🏁 Scripts executed

The following scripts were executed for the analysis:

Script:

#!/bin/bash
# Description: Check how getXdgCurrentDesktop is used in the codebase

# Find files importing getXdgCurrentDesktop
rg "import.*getXdgCurrentDesktop.*from.*platform" -A 5

# Find usage of XDG_CURRENT_DESKTOP in the codebase
rg "XDG_CURRENT_DESKTOP" -A 5

Length of output: 2484


Script:

#!/bin/bash
# Check if getXdgCurrentDesktop is exported from platform.ts
cat emain/platform.ts | grep -A 10 "export {"

Length of output: 268

Comment thread emain/platform.ts Resolved

@coderabbitai coderabbitai Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Actionable comments posted: 1

♻️ Duplicate comments (1)
emain/platform.ts (1)

203-211: ⚠️ Potential issue

Fix the return type of getXdgCurrentDesktop.

The function getXdgCurrentDesktop may return undefined, but its return type is declared as string. To accurately reflect the possible return values, the return type should be string | undefined.

Apply this diff to correct the return type:

-function getXdgCurrentDesktop(): string {
+function getXdgCurrentDesktop(): string | undefined {
🧹 Nitpick comments (1)
emain/platform.ts (1)

217-227: Refactor to eliminate code duplication between synchronous and asynchronous functions.

The functions callWithOriginalXdgCurrentDesktop and callWithOriginalXdgCurrentDesktopAsync share similar logic. Consider refactoring them to reduce duplication and improve maintainability.

One possible approach is to create a higher-order function that handles both sync and async callbacks:

function withOriginalXdgCurrentDesktop<T>(callback: () => T): T {
    const currXdgCurrentDesktopDefined = 'XDG_CURRENT_DESKTOP' in process.env;
    const currXdgCurrentDesktop = process.env.XDG_CURRENT_DESKTOP;
    const originalXdgCurrentDesktop = getXdgCurrentDesktop();

    process.env.XDG_CURRENT_DESKTOP = originalXdgCurrentDesktop;

    try {
        return callback();
    } finally {
        if (currXdgCurrentDesktopDefined) {
            process.env.XDG_CURRENT_DESKTOP = currXdgCurrentDesktop;
        } else {
            delete process.env.XDG_CURRENT_DESKTOP;
        }
    }
}

You can then use this function for both synchronous and asynchronous callbacks:

// For synchronous callbacks
withOriginalXdgCurrentDesktop(() => {
    // Your sync code here
});

// For asynchronous callbacks
await withOriginalXdgCurrentDesktop(async () => {
    // Your async code here
});

Also applies to: 233-243

📜 Review details

Configuration used: CodeRabbit UI
Review profile: CHILL
Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between e2d69c1 and c19df0a.

📒 Files selected for processing (2)
  • emain/emain.ts (3 hunks)
  • emain/platform.ts (1 hunks)
🚧 Files skipped from review as they are similar to previous changes (1)
  • emain/emain.ts
⏰ Context from checks skipped due to timeout of 90000ms (3)
  • GitHub Check: Analyze (javascript-typescript)
  • GitHub Check: Build for TestDriver.ai
  • GitHub Check: Analyze (go)

Comment thread emain/platform.ts Resolved
@esimkowitz
esimkowitz merged commit b7dca41 into main Jan 16, 2025
@esimkowitz
esimkowitz deleted the evan/x-www-browser branch January 16, 2025 19:37
xxyy2024 pushed a commit to xxyy2024/waveterm_aipy that referenced this pull request Jun 24, 2025
Electron sets XDG_CURRENT_DESKTOP for better Chromium compatibility on
Linux. We need to unset this for `wavesrv` and for any calls to open a
file or URL externally in `emain`.

See
https://www.electronjs.org/docs/latest/api/environment-variables#original_xdg_current_desktop

There's a bug open in Electron related to this:
electron/electron#45129

closes wavetermdev#1733
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[Bug]: system default browser not being honored

1 participant