Skip to content

[JS]: Handle optional dependency for @bazel/runfiles #14352

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 7 commits into from
Aug 10, 2024
Merged

[JS]: Handle optional dependency for @bazel/runfiles #14352

merged 7 commits into from
Aug 10, 2024

Conversation

harsha509
Copy link
Member

@harsha509 harsha509 commented Aug 7, 2024

User description

Description

In an npm project, requiring @bazel/runfiles with const { runfiles } = require('@bazel/runfiles') throws an exception because @bazel/runfiles is not available outside of Bazel builds.

The fix safely attempts to require @bazel/runfiles and logs a message if it fails.

Types of changes

  • Bug fix (non-breaking change which fixes an issue)
  • New feature (non-breaking change which adds functionality)
  • Breaking change (fix or feature that would cause existing functionality to change)

Checklist

  • I have read the contributing document.
  • My change requires a change to the documentation.
  • I have updated the documentation accordingly.
  • I have added tests to cover my changes.
  • All new and existing tests passed.

PR Type

Bug fix


Description

  • Removed direct import of @bazel/runfiles which caused exceptions in non-Bazel environments.
  • Added a try-catch block to safely attempt requiring @bazel/runfiles.
  • Logged informative error messages if @bazel/runfiles is not available.
  • Set runfiles to null if the module is not available to prevent further issues.

Changes walkthrough 📝

Relevant files
Bug fix
index.js
Handle optional dependency for `@bazel/runfiles` in tests

javascript/node/selenium-webdriver/testing/index.js

  • Removed direct import of @bazel/runfiles.
  • Added a try-catch block to optionally require @bazel/runfiles.
  • Logged error messages if @bazel/runfiles is not available.
  • Set runfiles to null if the module is not available.
  • +12/-1   

    💡 PR-Agent usage:
    Comment /help on the PR to get a list of all available PR-Agent tools and their descriptions

    @harsha509 harsha509 requested a review from shs96c August 7, 2024 13:57
    @qodo-merge-pro qodo-merge-pro bot added P-bug fix PR addresses a known issue Review effort [1-5]: 2 labels Aug 7, 2024
    Copy link
    Contributor

    qodo-merge-pro bot commented Aug 7, 2024

    PR Reviewer Guide 🔍

    ⏱️ Estimated effort to review: 2 🔵🔵⚪⚪⚪
    🧪 No relevant tests
    🔒 No security concerns identified
    ⚡ Key issues to review

    Error Handling
    Consider refining the error handling by specifying different actions or messages based on the type of error when requiring @bazel/runfiles. This can help in understanding the specific issue and how to resolve it, rather than a generic error message.

    Copy link
    Contributor

    qodo-merge-pro bot commented Aug 7, 2024

    PR Code Suggestions ✨

    CategorySuggestion                                                                                                                                    Score
    Maintainability
    Consolidate multiple error messages into a single console.error call for clarity

    Refactor the error handling for requiring @bazel/runfiles to avoid multiple
    console.error calls and consolidate the error message into a single output. This
    will make the error logs cleaner and more manageable.

    javascript/node/selenium-webdriver/testing/index.js [48-57]

     try {
       // Attempt to require @bazel/runfiles
       runfiles = require('@bazel/runfiles').runfiles;
     } catch (error) {
    -  // Handle error if @bazel/runfiles is not supported by mocha
    -  console.error('Error requiring @bazel/runfiles:', error.message);
    -  console.error('Note: If you are running tests with Mocha or Jasmine, this module is not needed.');
    -  console.error('For more details, see: https://github.com/bazelbuild/rules_nodejs/issues/3770');
    +  const errorMessage = `Error requiring @bazel/runfiles: ${error.message}\n` +
    +                       `Note: If you are running tests with Mocha or Jasmine, this module is not needed.\n` +
    +                       `For more details, see: https://github.com/bazelbuild/rules_nodejs/issues/3770`;
    +  console.error(errorMessage);
       runfiles = null; // Set to null if not available
     }
     
    • Apply this suggestion
    Suggestion importance[1-10]: 7

    Why: The suggestion improves code maintainability by consolidating multiple console.error calls into a single call, making the error logs cleaner and easier to manage. This is a minor improvement but beneficial for readability and debugging.

    7

    @pujagani
    Copy link
    Contributor

    pujagani commented Aug 8, 2024

    I have faced this few times. I think this looks good to me.

    Note: If you are running tests with Mocha or Jasmine, this module is not needed.\n
    For more details, see: https://github.com/bazelbuild/rules_nodejs/issues/3770`
    console.error(errorMessage)
    runfiles = null // Set to null if not available
    Copy link
    Member

    Choose a reason for hiding this comment

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

    Do we properly wrap all usages of runfiles later in this file? Maybe we do a check before we use it, and exit quickly if we know it'll never succeed?

    Copy link
    Member Author

    Choose a reason for hiding this comment

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

    Added a check for runfiles and implemented a safe, silent exit if not found.

    @harsha509 harsha509 requested a review from shs96c August 8, 2024 19:31
    @harsha509 harsha509 requested a review from shs96c August 10, 2024 10:23
    Copy link
    Member

    @shs96c shs96c left a comment

    Choose a reason for hiding this comment

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

    LGTM! Thank you!

    @harsha509 harsha509 merged commit aa4a41e into SeleniumHQ:trunk Aug 10, 2024
    10 of 11 checks passed
    Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
    Labels
    P-bug fix PR addresses a known issue Review effort [1-5]: 2
    Projects
    None yet
    Development

    Successfully merging this pull request may close these issues.

    3 participants