Skip to content

Commit

Permalink
fix: Read buid.target cargo config (#659)
Browse files Browse the repository at this point in the history
fixes #653
  • Loading branch information
FabianLars committed Jan 2, 2024
1 parent a929b44 commit 27089ad
Show file tree
Hide file tree
Showing 4 changed files with 60 additions and 12 deletions.
5 changes: 5 additions & 0 deletions .changes/read-build-target-config.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
action: 'patch'
---

The action now reads `build.target` from `.cargo/config` toml to get the correct `target` directory.
2 changes: 1 addition & 1 deletion dist/index.js

Large diffs are not rendered by default.

7 changes: 7 additions & 0 deletions src/types.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -69,3 +69,10 @@ export interface TauriConfig {
};
};
}

export interface CargoConfig {
build?: {
target?: string;
'target-dir'?: string;
};
}
58 changes: 47 additions & 11 deletions src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,13 @@ import { globbySync } from 'globby';

import { getConfig, mergePlatformConfig, mergeUserConfig } from './config';

import type { CargoManifest, Info, TargetInfo, TargetPlatform } from './types';
import type {
CargoConfig,
CargoManifest,
Info,
TargetInfo,
TargetPlatform,
} from './types';

/*** constants ***/
export const extensions = [
Expand Down Expand Up @@ -108,30 +114,60 @@ export function getWorkspaceDir(dir: string): string | null {
}

export function getTargetDir(crateDir: string): string {
// The default path if no configs are set.
const def = join(crateDir, 'target');
if ('CARGO_TARGET_DIR' in process.env) {
return process.env.CARGO_TARGET_DIR ?? def;
}

// This will hold the path of current iteration
let dir = crateDir;

// hold on to target-dir cargo config while we search for build.target
let targetDir;
// same for build.target
let targetDirExt;

// The env var takes precedence over config files.
if (process.env.CARGO_TARGET_DIR) {
targetDir = process.env.CARGO_TARGET_DIR ?? def;
}

while (dir.length && dir[dir.length - 1] !== sep) {
let cargoConfigPath = join(dir, '.cargo/config');
if (!existsSync(cargoConfigPath)) {
cargoConfigPath = join(dir, '.cargo/config.toml');
}
if (existsSync(cargoConfigPath)) {
const cargoConfig = parseToml(readFileSync(cargoConfigPath).toString());
// @ts-ignore
if (cargoConfig.build?.['target-dir']) {
// @ts-ignore
const cargoConfig = parseToml(
readFileSync(cargoConfigPath).toString(),
) as CargoConfig;

if (!targetDir && cargoConfig.build?.['target-dir']) {
const t = cargoConfig.build['target-dir'];
if (path.isAbsolute(t)) return t;
return normalize(join(dir, t));
if (path.isAbsolute(t)) {
targetDir = t;
} else {
targetDir = normalize(join(dir, t));
}
}

// Even if build.target is the same as the default target it will change the output dir.
// Just like tauri we only support a single string, not an array (bug?).
if (!targetDirExt && typeof cargoConfig.build?.target === 'string') {
targetDirExt = cargoConfig.build.target;
}
}

// If we got both we don't need to keep going
if (targetDir && targetDirExt) break;

// Prepare the path for the next iteration
dir = normalize(join(dir, '..'));
}
return def;

if (targetDir) {
return normalize(join(targetDir, targetDirExt ?? ''));
}

return normalize(join(def, targetDirExt ?? ''));
}

export function getCargoManifest(dir: string): CargoManifest {
Expand Down

0 comments on commit 27089ad

Please sign in to comment.