Skip to content

Feature/add parent dir check#11

Merged
ScottGibb merged 21 commits intomainfrom
feature/add-parent-dir-check
Jul 16, 2025
Merged

Feature/add parent dir check#11
ScottGibb merged 21 commits intomainfrom
feature/add-parent-dir-check

Conversation

@ScottGibb
Copy link
Contributor

@ScottGibb ScottGibb commented Jul 4, 2025

Description

This PR Aims to add the ability for cargo metadata to handle git repos in which the cargo.toml file is in a subdirectory. It also aims to test out the cargo metadata functionality inside Docker, Balena and MacOS

Type of change

Please Tick the options that are relevant.

  • Bug fix (non-breaking change which fixes an issue)
  • New feature (non-breaking change which adds functionality)
  • Breaking change (fix or feature that would cause existing functionality to not work as expected)
  • This change requires a documentation update

How Has This Been Tested?

Tested on MacOS

Your Specific Test Case

Checklist

  • My code follows the style guidelines of this project (this should be caught by the CI)
  • I have performed a self-review of my own code
  • I have commented my code, particularly in hard-to-understand areas
  • I have made corresponding changes to the documentation
  • My changes generate no new warnings (this should be caught by the CI)
  • I have added tests that prove my fix is effective or that my feature works
  • New and existing unit tests pass locally with my changes

@ScottGibb
Copy link
Contributor Author

When running on MacOS
fn main() {
println!("cargo:rerun-if-changed=./.git/");
// println!("cargo:rustc-link-arg=-Tmetadata.x");
}

@github-actions
Copy link
Contributor

github-actions bot commented Jul 4, 2025

🦙 MegaLinter status: ⚠️ WARNING

Descriptor Linter Files Fixed Errors Warnings Elapsed time
✅ ACTION actionlint 3 0 0 0.02s
✅ JSON jsonlint 3 0 0 0.13s
✅ JSON prettier 3 0 0 0 0.33s
⚠️ MARKDOWN markdownlint 6 0 4 0 0.56s
✅ MARKDOWN markdown-link-check 6 0 0 3.33s
✅ MARKDOWN markdown-table-formatter 6 0 0 0 0.19s
✅ RUST clippy yes no no 49.32s
✅ YAML prettier 5 0 0 0 0.36s
✅ YAML yamllint 5 0 0 0.4s

See detailed report in MegaLinter reports

MegaLinter is graciously provided by OX Security

@ScottGibb
Copy link
Contributor Author

Addressing Issue
#1
and
#9

@petekubiak
Copy link
Contributor

When running on MacOS
fn main() {
println!("cargo:rerun-if-changed=./.git/");
// println!("cargo:rustc-link-arg=-Tmetadata.x");
}

For the first line we'll need to sub in whatever the path to the git folder is based on the find_parent() function.

As for the second, you'll need something like that, but it might need adjusting for Mac. The linker script is there to ensure that the section is always included in the binary, even if you don't reference it in your code (otherwise the linker throws it away)

@ScottGibb
Copy link
Contributor Author

@doublebasil

@ScottGibb
Copy link
Contributor Author

// println!("cargo:rustc-link-arg=-Tmetadata.x");

#[cfg(not(target_os = "macos"))]
println!("cargo:rustc-link-arg=-Tmetadata.x");

@ScottGibb
Copy link
Contributor Author

Need to investigate further into Mac Linkers and making sure we link correclty. There seems to be no command line option for declaring this...

@ScottGibb
Copy link
Contributor Author

@petekubiak I believe this works but im unsure how I can get the git_path and put it into build.rs for example:

within lib.rs

fn find_valid_git_root(mut path: &str) -> Result<Repository, Error> {
    let mut depth = 0;
    loop {
        depth += 1;
        // Find last path separator position
        let pos_slash = path.rfind('/');
        let pos_backslash = path.rfind('\\');
        let last = match (pos_slash, pos_backslash) {
            (Some(s), Some(b)) => core::cmp::max(s, b),
            (Some(s), None) => s,
            (None, Some(b)) => b,
            (None, None) => break, // Reached the top level
        };

        // Take the parent part
        path = &path[..last];

        // If this is a valid repository return it otherwise keep going
        if let Ok(repo) = Repository::open(path) {
            // Tell build.rs what the path is?
            return Ok(repo);
        }
    }
    Err(Error::new(
        Span::call_site(),
        format!("failed to find .git folder after {depth} parents"),
    ))
}

Theres that function ^. However in build.rs I was going to an env variable to get the GIT_DIR

use std::env;

fn main() {
    let git_dir = env::var("PROJECT_GIT_DIR").unwrap_or_else(|_| "./.git/".to_string());
    println!("cargo:rerun-if-changed={}", git_dir);
    #[cfg(not(target_os = "macos"))]
    println!("cargo:rustc-link-arg=-Tmetadata.x");
}

But then I cant use env inside the crate because its no-std . Do you have any thoughts?

@petekubiak
Copy link
Contributor

I think I'd need to have a proper chat with you to understand the issue. Let's catch up on Monday :)

Co-authored-by: petekubiak <37507920+petekubiak@users.noreply.github.com>
@ScottGibb ScottGibb marked this pull request as ready for review July 14, 2025 19:31
Copy link
Contributor

@petekubiak petekubiak left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Couple minor tweaks - thanks for your work on this!

Co-authored-by: petekubiak <37507920+petekubiak@users.noreply.github.com>
ScottGibb and others added 3 commits July 15, 2025 10:20
Co-authored-by: petekubiak <37507920+petekubiak@users.noreply.github.com>
Co-authored-by: petekubiak <37507920+petekubiak@users.noreply.github.com>
@ScottGibb ScottGibb merged commit 3c4449d into main Jul 16, 2025
7 checks passed
@ScottGibb ScottGibb deleted the feature/add-parent-dir-check branch July 16, 2025 11:05
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