Skip to content

Commit

Permalink
Fix workspace Cargo.toml and generated CI (#67)
Browse files Browse the repository at this point in the history
  • Loading branch information
lorisleiva authored Jul 23, 2024
1 parent dacc3ac commit 8c09a9b
Show file tree
Hide file tree
Showing 14 changed files with 94 additions and 40 deletions.
5 changes: 5 additions & 0 deletions .changeset/fluffy-seas-walk.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"create-solana-program": patch
---

Fix workspace Cargo.toml and generated CI
19 changes: 15 additions & 4 deletions .github/workflows/main.yml
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,8 @@ on:

env:
ANCHOR_VERSION: 0.30.0
SOLANA_VERSION: 1.18.12
SOLANA_VERSION: 1.18.18
TOOLCHAIN: 1.75.0

jobs:
lint:
Expand All @@ -35,14 +36,14 @@ jobs:
strategy:
matrix:
project: ["counter-anchor", "counter-shank"]
solana: ["1.17.24", "1.18.12"]
solana: ["1.17.34", "1.18.18"]
include:
- anchor: "0.30.0"
project: "counter-anchor"
solana: "1.18.12"
solana: "1.18.18"
- anchor: "0.29.0"
project: "counter-anchor"
solana: "1.17.24"
solana: "1.17.34"
steps:
- name: Git checkout
uses: actions/checkout@v4
Expand All @@ -61,6 +62,16 @@ jobs:
uses: metaplex-foundation/actions/install-solana@v1
with:
version: ${{ matrix.solana }}
- name: Install Rustfmt
uses: dtolnay/rust-toolchain@master
with:
toolchain: ${{ env.TOOLCHAIN }}
components: rustfmt
- name: Install Clippy
uses: dtolnay/rust-toolchain@master
with:
toolchain: ${{ env.TOOLCHAIN }}
components: clippy
- name: Install Anchor
if: ${{ matrix.anchor }}
uses: metaplex-foundation/actions/install-anchor-cli@v1
Expand Down
6 changes: 0 additions & 6 deletions template/anchor/base/Cargo.toml

This file was deleted.

2 changes: 1 addition & 1 deletion template/anchor/base/program/Cargo.toml.njk
Original file line number Diff line number Diff line change
Expand Up @@ -21,4 +21,4 @@ idl-build = ["anchor-lang/idl-build"]

[dependencies]
anchor-lang = "{{ anchorVersion }}"
solana-program = "~{{ solanaVersion }}"
solana-program = "~{{ solanaVersionWithoutPatch }}"
6 changes: 0 additions & 6 deletions template/anchor/clients/rust/Cargo.toml

This file was deleted.

2 changes: 1 addition & 1 deletion template/base/.github/actions/setup/action.yml.njk
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ runs:
shell: bash
run: pnpm zx ./scripts/ci/set-env.mjs

{% if solanaVersion === '2.0' %}
{% if solanaVersionWithoutPatch === '2.0' %}
- name: Install Protobuf Compiler (Temporary Workaround for Solana 2.0)
if: {% raw %}${{ inputs.solana || inputs.rustfmt == 'true' || inputs.clippy == 'true' }}{% endraw %}
shell: bash
Expand Down
4 changes: 2 additions & 2 deletions template/base/.github/workflows/main.yml.njk
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,9 @@ on:

env:
NODE_VERSION: 18
SOLANA_VERSION: 1.18.12
SOLANA_VERSION: {{ solanaVersion }}
{% if programFramework === 'anchor' %}
ANCHOR_VERSION: 0.30.0
ANCHOR_VERSION: {{ anchorVersion }}
{% endif %}

jobs:
Expand Down
9 changes: 9 additions & 0 deletions template/base/Cargo.toml.njk
Original file line number Diff line number Diff line change
@@ -1,6 +1,15 @@
[workspace]
resolver = "2"
{% if rustClient %}
members = ["clients/rust", "program"]
{% else %}
members = ["program"]
{% endif %}

{% if programFramework === 'anchor' %}
[profile.release]
overflow-checks = true
{% endif %}

# Specify Rust toolchains for rustfmt, clippy, and build.
# Any unprovided toolchains default to stable.
Expand Down
3 changes: 0 additions & 3 deletions template/clients/rust/Cargo.toml

This file was deleted.

6 changes: 3 additions & 3 deletions template/clients/rust/clients/rust/Cargo.toml.njk
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,10 @@ num-derive = "^0.3"
num-traits = "^0.2"
serde = { version = "^1.0", features = ["derive"], optional = true }
serde_with = { version = "^3.0", optional = true }
solana-program = "~{{ solanaVersion }}"
solana-program = "~{{ solanaVersionWithoutPatch }}"
thiserror = "^1.0"

[dev-dependencies]
assert_matches = "1.5.0"
solana-program-test = "~{{ solanaVersion }}"
solana-sdk = "~{{ solanaVersion }}"
solana-program-test = "~{{ solanaVersionWithoutPatch }}"
solana-sdk = "~{{ solanaVersionWithoutPatch }}"
2 changes: 1 addition & 1 deletion template/shank/base/program/Cargo.toml.njk
Original file line number Diff line number Diff line change
Expand Up @@ -19,5 +19,5 @@ borsh = "^0.10"
shank = "^0.4.2"
num-derive = "^0.3"
num-traits = "^0.2"
solana-program = "~{{ solanaVersion }}"
solana-program = "~{{ solanaVersionWithoutPatch }}"
thiserror = "^1.0"
54 changes: 46 additions & 8 deletions utils/getRenderContext.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ export type RenderContext = Omit<Inputs, 'programAddress' | 'solanaVersion'> & {
packageManager: PackageManager;
solanaVersion: string;
solanaVersionDetected: string;
solanaVersionWithoutPatch: string;
targetDirectory: string;
templateDirectory: string;
toolchain: string;
Expand All @@ -45,10 +46,16 @@ export function getRenderContext({
);
const getNpmCommand: RenderContext['getNpmCommand'] = (...args) =>
getPackageManagerCommand(packageManager, ...args);
const solanaVersion =
inputs.solanaVersion ??
toMinorSolanaVersion(language, solanaVersionDetected);
const toolchain = getToolchainFromSolanaVersion(solanaVersion);
const solanaVersion = resolveSolanaVersion(
language,
inputs.solanaVersion,
solanaVersionDetected
);
const solanaVersionWithoutPatch = toMinorSolanaVersion(
language,
solanaVersion
);
const toolchain = getToolchainFromSolanaVersion(solanaVersionWithoutPatch);

// Directories.
const templateDirectory = path.resolve(__dirname, 'template');
Expand All @@ -62,7 +69,7 @@ export function getRenderContext({

return {
...inputs,
anchorVersion: anchorVersionDetected ?? '',
anchorVersion: resolveAnchorVersion(anchorVersionDetected),
clientDirectory,
clients,
currentDirectory,
Expand All @@ -73,18 +80,49 @@ export function getRenderContext({
programDirectory,
solanaVersion,
solanaVersionDetected,
solanaVersionWithoutPatch,
targetDirectory,
templateDirectory,
toolchain,
};
}

function getToolchainFromSolanaVersion(solanaVersion: string): string {
function getToolchainFromSolanaVersion(
solanaVersionWithoutPatch: string
): string {
const map: Record<string, string> = {
'1.17': '1.68.0',
'1.17': '1.75.0',
'1.18': '1.75.0',
'2.0': '1.75.0',
};

return map[solanaVersion] ?? '1.75.0';
return map[solanaVersionWithoutPatch] ?? '1.75.0';
}

function resolveSolanaVersion(
language: Language,
inputVersion: string | undefined,
detectedVersion: string
): string {
if (!inputVersion) {
return detectedVersion;
}
if (!inputVersion.match(/^\d+\.\d+(\.\d+)?$/)) {
throw new Error(
language.errors.invalidSolanaVersion.replace('$version', inputVersion)
);
}
const versionSegments = inputVersion.split('.');
if (versionSegments.length === 3) {
return inputVersion;
}
const map: Record<string, string> = {
'1.17': '1.17.34',
'1.18': '1.18.18',
};
return map[inputVersion] ?? `${inputVersion}.0`;
}

function resolveAnchorVersion(detectedVersion: string | undefined): string {
return detectedVersion ?? '';
}
8 changes: 7 additions & 1 deletion utils/runCommands.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,15 @@ export async function hasCommand(command: string): Promise<boolean> {

export async function waitForCommand(child: ChildProcess): Promise<number> {
return new Promise((resolve, reject) => {
const errorLogs: string[] = [];
child.stderr?.on('data', (data) => {
errorLogs.push(data.toString());
});

child.on('close', (code) => {
if (code !== 0) {
const message = `$(${child}) exited with code ${code}`;
console.log(errorLogs.join(''));
const message = `$(${child.spawnargs.join(' ')}) exited with code ${code}`;
reject(new Error(message));
} else {
resolve(code);
Expand Down
8 changes: 4 additions & 4 deletions utils/solanaCli.ts
Original file line number Diff line number Diff line change
Expand Up @@ -44,15 +44,15 @@ export async function detectAnchorVersion(language: Language): Promise<string> {
}

export async function patchSolanaDependencies(
ctx: Pick<RenderContext, 'solanaVersion' | 'targetDirectory'>
ctx: Pick<RenderContext, 'solanaVersionWithoutPatch' | 'targetDirectory'>
): Promise<void> {
const patchMap: Record<string, string[]> = {
'1.17': ['-p ahash@0.8 --precise 0.8.6'],
'1.17': ['-p ahash@0.8.11 --precise 0.8.6'],
};

const patches = patchMap[ctx.solanaVersion] ?? [];
const patches = patchMap[ctx.solanaVersionWithoutPatch] ?? [];
await Promise.all(
patches.map((patch) =>
patches.map(async (patch) =>
waitForCommand(
spawnCommand('cargo', ['update', ...patch.split(' ')], {
cwd: ctx.targetDirectory,
Expand Down

0 comments on commit 8c09a9b

Please sign in to comment.