Skip to content

cargo::ops::package returns incorrect .crate file path #7863

@lily-mara

Description

@lily-mara

Problem
cargo::ops::package creates a tarball of a crate for publishing/uploading purposes. It returns a cargo::util::FileLock that should point to the generated tarball. In fact, it points to the scratch file that cargo uses to write the tarball, but this scratch file is deleted before the function ends.

Steps
This code illustrates the bug:

use cargo::core::{shell::Shell, Workspace};
use cargo::ops::PackageOpts;
use cargo::util::config;
use cargo::util::config::Config;
use std::env;

fn main() {
    let dir = env::current_dir().unwrap();

    let homedir = config::homedir(&dir).unwrap();

    let config = Config::new(Shell::new(), dir.clone(), homedir.clone());

    let workspace = Workspace::new(&dir.join("Cargo.toml"), &config).unwrap();

    let x = cargo::ops::package(
        &workspace,
        &PackageOpts {
            config: &config,
            list: false,
            check_metadata: false,
            allow_dirty: true,
            verify: false,
            jobs: None,
            target: None,
            features: Vec::new(),
            all_features: false,
            no_default_features: false,
        },
    )
    .unwrap()
    .unwrap();

    let path = x.path();

    println!("Path = {}", path.display());
    println!("Exists = {}", path.exists());
}

Expectation
The path returned by ops::package should exist

Reality
The returned path is deleted before the function returns

Possible Solution(s)
Replace the path field on the FileLock before package returns at the time the file is renamed.

fs::rename(&src_path, &dst_path)

Notes

Output of cargo version:

libcargo 1.42.0

Metadata

Metadata

Assignees

Labels

A-cargo-apiArea: cargo-the-library API and internal code issuesA-filesystemArea: issues with filesystemsC-bugCategory: bugS-acceptedStatus: Issue or feature is accepted, and has a team member available to help mentor or review

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions