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

"strict transitive dependency mode" not work ? #15632

Closed
daohu527 opened this issue Jun 7, 2022 · 6 comments
Closed

"strict transitive dependency mode" not work ? #15632

daohu527 opened this issue Jun 7, 2022 · 6 comments
Labels
P4 This is either out of scope or we don't have bandwidth to review a PR. (No assignee) team-Rules-CPP Issues for C++ rules type: support / not a bug (process)

Comments

@daohu527
Copy link

daohu527 commented Jun 7, 2022

Description of the bug:

Suppose there is the following dependency a->b->c, a contains the header file of c, but does not use anything, then a's build file depends on b, but not on c. Then this forms an implicit dependency, and bazel will not report a error!

What's the simplest, easiest way to reproduce this bug? Please provide a minimal example if possible.

No response

Which operating system are you running Bazel on?

ubuntu 18.04

What is the output of bazel info release?

(11:37:20) INFO: Invocation ID: b93b0d49-edac-4215-9d9a-366bf997e477 release 3.7.1

If bazel info release returns development version or (@non-git), tell us how you built Bazel.

No response

What's the output of git remote get-url origin; git rev-parse master; git rev-parse HEAD ?

No response

Have you found anything relevant by searching the web?

No response

Any other information, logs, or outputs that you want to share?

No response

@sgowroji
Copy link
Member

sgowroji commented Jun 7, 2022

Hello @daohu527, Can you provide more details to reproduce the above problem. Thanks!

@daohu527
Copy link
Author

daohu527 commented Jun 8, 2022

I modified https://github.com/bazelbuild/examples/tree/main/cpp-tutorial/stage3 as an example.

as you see hello-world.cc contain lib/hello-time.h but no use it. and then hello-world deps on hello-greet, which deps on //lib:hello-time. In other words hello-world implicit deps on //lib:hello-time, but bazel will success build, which violates the strict transitive dependency mode.

additional, hello-world.cc use print_localtime will also success build.

cpp-tutorial/stage3/main/BUILD

load("@rules_cc//cc:defs.bzl", "cc_binary", "cc_library")

cc_library(
    name = "hello-greet",
    srcs = ["hello-greet.cc"],
    hdrs = ["hello-greet.h"],
    deps = [
        "//lib:hello-time",
    ],
)

cc_binary(
    name = "hello-world",
    srcs = ["hello-world.cc"],
    deps = [
        ":hello-greet",
    ],
)

cpp-tutorial/stage3/main/hello-world.cc

#include "lib/hello-time.h"
#include "main/hello-greet.h"
#include <iostream>
#include <string>

int main(int argc, char** argv) {
  std::string who = "world";
  if (argc > 1) {
    who = argv[1];
  }
  std::cout << get_greet(who) << std::endl;
  // print_localtime();
  return 0;
}

@fmeum
Copy link
Collaborator

fmeum commented Jun 8, 2022

Strict deps checking for C++ on that level of granularity requires passing the flag --features=layering_check as it may not work with all compilers. Does that give you the expected failure?

@daohu527
Copy link
Author

daohu527 commented Jun 8, 2022

yes, it thrown out an error like below.

main/hello-world.cc:1:10: error: module //main:hello-world does not depend on a module exporting 'lib/hello-time.h'
#include "lib/hello-time.h"

Then my question is, since bazel lets users use strict transitive dependency in best practice, why doesn't it check by default? link

@oquenchil oquenchil added P4 This is either out of scope or we don't have bandwidth to review a PR. (No assignee) and removed untriaged labels Jul 7, 2022
@martis42
Copy link

@daohu527 This project of mine might be interesting for you: https://github.com/martis42/depend_on_what_you_use

It aims at linting the usage of headers in Bazel C++ targets compared to the targets dependency list.

It is still in an early phase, but maybe it can help you.

@daohu527
Copy link
Author

@martis42 Thanks for your work, I will check out this project in detail !

hvadehra pushed a commit that referenced this issue Feb 14, 2023
The `layering_check` feature and toolchain support are required to realize header dependency checking in Bazel. If enabled, this also reports errors for explicitly included transitive, non-direct headers.

Fixes #17055
Fixes #15632

Closes #17057.

PiperOrigin-RevId: 499189567
Change-Id: Ia7978193e4572a358e26d24183d125649f8654b9
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
P4 This is either out of scope or we don't have bandwidth to review a PR. (No assignee) team-Rules-CPP Issues for C++ rules type: support / not a bug (process)
Projects
None yet
Development

Successfully merging a pull request may close this issue.

5 participants