Single project - multiple source dirs (newbie)

Hi, well, question is probably basic, I've googled a bit, but decided to post here as googling might or might not lead to proper conclusion.
I am using plain Swift setup on Linux (no IOS/Xcode or other mumbo jumbo) things. Simple Swift project, developing with VS Code. (everything works fine build, code completion, debugging etc)

Now I have structure like this
HelloSwift
+--Source
+------ HelloSwift
+------ ErrorHandling

I am using it for learning purpose etc.
HelloSwift subfolder was created automatically by Swift package manager.
I added ErrorHandling folder and put some code into it.

To use ErrorHandling stuff in HelloSwift code I had to add target (googling and trial and error approach led mi there) to Package.swift (this way project builds no problemo):

            .target(
              name: "ErrorHandling", 
              dependencies: []),
            .target(
                name: "HelloSwift",
                dependencies: ["ErrorHandling"]),

and
import ErrorHandling
where I want to use it in HelloSwift

Now - the question is... is this really right way? I mean adding subfolder of Source folder as target
effectively make it module and well... I can't say it is because I am not sure but seems like too
much to have to add subfolder as target to be able to use it from other source folder in the same project :confused:
I have worked with many different platforms and if subfolder of Swift project source is what is ...I don't know... Maven module within maven project or one of the projects within .net solution then...OK... I am just checking.

Any other way is more errr how to say.... "Swifty"?

(I have read though about naming files like: HigherLevelConcept+Feature.swift
and putting into same folder but, well, I wanted clearer and more explicit this way of structuring)

Thanks in advance

Each .target you declare in the manifest becomes its own module. Each .target also has its own directory. By default, a target’s directory is assumed to be at Sources/<TargetName>, and everything it contains belongs to the target.

It sounds to me like you want something like this, with only a single “HelloSwift” target:

  • (Package Root)
    • Package.swift (manifest)
    • Sources
      • HelloSwift (the target)
        • Core (a subdirectory for the primary stuff)
          • BlahBlah.swift
          • Something.swift
        • ErrorHandling (a subdirectory for your error stuff)
          • ErrorOne.swift
          • ErrorTwo.swift

But that is just the customary default. You can rearrange it in other ways if necessary by specifying your layout in the manifest. The documentation is here.

1 Like

Aha, so, everything under folder(specified as target) will be recursively picked up?
Well, for my current use case...definitely better option.

Documentation link is also very helpful, I will check it out for better insight
and self-mind-clarification what actually I want :slight_smile:
I am still getting grasp on how swift build and package management differs or is similar
in behavior to e.g. Java's maven and similar tools...

Thanks!

Terms of Service

Privacy Policy

Cookie Policy