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

ts: Fix loading programs with numbers in their names using workspace #3450

Conversation

acheroncrypto
Copy link
Collaborator

Problem

Using numbers in program identifiers might result in problems when loading the program using anchor.workspace as explained here:

// Check whether the program name contains any digits
if (/\d/.test(programName)) {
// Numbers cannot be properly converted from camelCase to snake_case,
// e.g. if the `programName` is `myProgram2`, the actual program name could
// be `my_program2` or `my_program_2`. This implementation assumes the
// latter as the default and always converts to `_numbers`.
//
// A solution to the conversion of program names with numbers in them
// would be to always convert the `programName` to camelCase instead of
// snake_case. The problem with this approach is that it would require
// converting everything else e.g. program names in Anchor.toml and IDL
// file names which are both snake_case.
programName = programName
.replace(/\d+/g, (match) => "_" + match)
.replace("__", "_");
}

When I initially was working on the above change, I first made it assume there is no _ involved e.g. if the user gives myProgram2, the program name is my_program2. However, with this change, the examples started failing because their names are basic_<number>, which then made me change it to its current form.

For example, if the program name is program_v2, and it's getting loaded with anchor.workspace.programV2, it results in the following error:

Error: target/idl/program_v_2.json doesn't exist. Did you run `anchor build`?

This is also one of the most commonly reported problems.

Summary of changes

Fix loading programs with numbers in their names using anchor.workspace.

Resolves #3353

Copy link

vercel bot commented Dec 24, 2024

@acheroncrypto is attempting to deploy a commit to the coral-xyz Team on Vercel.

A member of the Team first needs to authorize it.

@acheroncrypto acheroncrypto added ts fix Bug fix PR labels Dec 24, 2024
@acheroncrypto acheroncrypto merged commit 72c7e09 into coral-xyz:master Dec 25, 2024
52 of 53 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
fix Bug fix PR ts
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Anchor fails to create expected IDL name
1 participant