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: add try/catch on machineId() #7538

Closed

Conversation

josemanosalvas
Copy link

@josemanosalvas josemanosalvas commented Sep 23, 2024

Description

This PR addresses an issue with the @sanity/cli package, which does not function correctly with turbo version 2.0 or higher on Windows systems. The root of the issue lies in the use of the node-machine-id dependency for telemetry purposes. When run on Windows with turbo, an error is thrown:

Error: Error while obtaining machine id: Error: Command failed: %windir%\System32\REG.exe QUERY HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Cryptography /v MachineGuid
The system cannot find the path specified.

This error persists even when telemetry is disabled, as the machineId function is still invoked. The PR resolves the issue by wrapping the machineId call in a try/catch block to handle the error gracefully.

Before:

telemetry.updateUserProperties({
  deviceId: await machineId(),
  // other properties...
});

After:

async function getDeviceId() {
  try {
    return await machineId();
  } catch (error) {
    console.error('Failed to get device ID:', error);
    return 'unknown device id';
  }
}

telemetry.updateUserProperties({
  deviceId: await getDeviceId(),
  // other properties...
});

What to review

Review the changes in how machineId is handled within the telemetry update. Specifically, focus on the new getDeviceId function and ensure that it appropriately handles errors without disrupting telemetry or causing other side effects.

  • Affected package: @sanity/cli
  • Flow to test: Telemetry data generation on Windows systems when using turbo v2.0+.

Testing

Manual testing was performed on a Windows system using turbo version 2.0. The error previously thrown by the node-machine-id library was resolved by the new getDeviceId function. As this is a Windows-specific issue, the testing was focused on this environment.

No automated tests were added due to the nature of the issue being specific to the Windows OS. However, manual tests confirmed that telemetry still functions as expected with and without valid machine IDs.

Notes for release

This change fixes compatibility between the @sanity/cli package and turbo v2.0+ on Windows systems.

  • This PR is required for users running turbo v2.0+ on Windows.
  • The key change is the addition of a fallback mechanism for obtaining device IDs in telemetry, which avoids crashes related to the node-machine-id dependency.

Copy link

vercel bot commented Sep 23, 2024

The latest updates on your projects. Learn more about Vercel for Git ↗︎

Name Status Preview Comments Updated (UTC)
page-building-studio ✅ Ready (Inspect) Visit Preview 💬 Add feedback Sep 23, 2024 3:40pm
performance-studio ✅ Ready (Inspect) Visit Preview Sep 23, 2024 3:40pm
1 Skipped Deployment
Name Status Preview Comments Updated (UTC)
studio-workshop ⬜️ Ignored (Inspect) Visit Preview Sep 23, 2024 3:40pm

Copy link

vercel bot commented Sep 23, 2024

@josemanosalvas is attempting to deploy a commit to the Sanity Sandbox Team on Vercel.

A member of the Team first needs to authorize it.

@josemanosalvas josemanosalvas marked this pull request as ready for review September 23, 2024 17:34
@josemanosalvas josemanosalvas requested a review from a team as a code owner September 23, 2024 17:34
@josemanosalvas josemanosalvas requested review from cngonzalez and removed request for a team September 23, 2024 17:34
@josemanosalvas josemanosalvas changed the title fix: add try-catch on machineId() fix: add try/catch on machineId() Sep 23, 2024
@bjoerge
Copy link
Member

bjoerge commented Sep 25, 2024

Hi @josemanosalvas

This PR sparked an internal discussion which led us to decide to remove our usage of node-machine-id entirely. See #7545.
Really appreciate the time and effort you spent on investigating and putting together the otherwise great fix for this issue!

@bjoerge bjoerge closed this Sep 25, 2024
@josemanosalvas
Copy link
Author

Hi @josemanosalvas

This PR sparked an internal discussion which led us to decide to remove our usage of node-machine-id entirely. See #7545. Really appreciate the time and effort you spent on investigating and putting together the otherwise great fix for this issue!

Awesome, thanks a lot for the quick resolution on this, happy to been able to help :)

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.

2 participants