Include same file in multiple packages

I have a uvm testbench that requires two agents. Both agents run the same sequence ( two different sequence instances run on two different sequencers). I created a package for each agent and included the sequence file in both similar to this code.

package agent1_pkg;
  ...
  `include "my_sequence.sv"
  ...
endpackage
package agent2_pkg;
  ...
  `include "my_sequence.sv"
  ...
endpackage

The agents packages are included in an env_pkg as well as in the main test_pkg along with the env_pkg.

package env_pkg;
  ...
  import agent1_pkg::*;
  import agent2_pkg::*;
  ...
endpackage
package test_pkg;
  ...
  import agent1_pkg::*;
  import agent2_pkg::*;
  import env_pkg::*;
  `include "my_test.sv"
  ...
endpackage

The test my_test tries to instantiate my_sequance. when compiling the test_pkg the following error shows up

** at my_test.sv(5): Type Identifier 'my_sequence' is not directly visible.
   Found multiple Declaration of 'my_sequence' through wildcard imports from these packages : agent1_pkg, agent2_pkg, agent2_pkg

My explanation is that the package in systemverilog has its own scope and when we import it with ::* we can access it directly without the need of scope resolution. Now that both packages import the same class we can’t access my_sequence directly as it exist in two different scopes and the compiler doesn’t know which one to choose.
I have some questions
1- Is my explanation right?
2- Since env_pkg import the agent_pkg it should already have the scope of the agent_pkg so why do I need to import the agent_pkg again?
3- What are possible solutions to this problem and are their any better ways to do this structure?

1 Like

In reply to M.Younis:

Using `include to add the same class to two different packages is a bad idea. This will certainly mess up the UVM factory registration by name. See SystemVerilog Coding Guidelines: Package import versus `include - Verification Horizons

Your explanation is mostly correct except that even if you use the wildcard import for the two agent packages, you can still explicitly reference agent_1::my_sequence or agent_2::my sequence without importing it.

It seems odd that you would have two different agents using the same sequences, but if that is what you need, you could put my_sequence in a common package and import it into the two agent packages. There is no need to import a package unless you plan to directly reference something in that package with you using an explicit package scope operator ::. So there may be no need for the test_pkg to import the agent pkg.

There is a package export capability that allows you to essentially chain package imports, but I only see that needed in rare cases.