figma / code-connect

A tool for connecting your design system components in code with your design system in Figma
MIT License
979 stars 70 forks source link

"Found Code Connect Swift package" step finding incorrect location with Swift Package Manager #140

Open jordikitto opened 3 months ago

jordikitto commented 3 months ago

Hi,

When running figma connect commands in my Xcode project root directory, I get the following:

Using "swift" parser as a file matching *.xcodeproj or Package.swift was found in /Users/jordikitto/Repos/ios-jupiter. If this is incorrect, please check you are running Code Connect from your project root, or add a `parser` key to your config file. See https://github.com/figma/code-connect for more information.
Fetching component information from Figma...
Parsing response
Generating Code Connect files...
Found Code Connect Swift package at /Users/jordikitto/Repos/<project>/build/../../SourcePackages/checkouts/code-connect, building parser binary. This may take a few minutes if this is the first time you've run Code Connect.
Failed to create: Parser exited with code 1

It fails because code-connect is not checked out to this location. I am using Swift Package Manager (SPM) and the actual location is:

/Users/jordikitto/Library/Developer/Xcode/DerivedData/<app>-aujmsoebpvanokfwonszghsvtqth/SourcePackages/checkouts/code-connect

Looking at your implementation, I see the following steps are occurring:

  1. xcodeProjFile is being defined via getFileIfExists(cwd, '*.xcodeproj')
  2. xcodebuild -project ${xcodeProjFile} -showBuildSettings is being run to determine:
    • If the Code Connect Swift package has been added (it has been)
    • Whether the package is local (it isn't)
    • What the BUILD_DIR is (/Users/jordikitto/Repos/<project>/build)
  3. The final path is then being constructed as /Users/jordikitto/Repos/<project>/build/../../SourcePackages/checkouts/code-connect

Again, unfortunately, this path is not the correct location. The correct location is in the DerivedData folder located above.

From my understanding, this is because of Swift Package Manager checking out the repo? Does Code Connect support SPM installation? If not, what is the correct way to use Code Connect with Xcode projects/workspaces?

Happy to provide any further details.

--

I did manage to get Code Connect to work with a hack symbolic link, for reference:

ln -s /Users/jordikitto/Library/Developer/Xcode/DerivedData/<app>-aujmsoebpvanokfwonszghsvtqth/SourcePackages/checkouts/code-connect /Users/jordikitto/Repos/SourcePackages/checkouts/code-connect

Please provide:

jyyang0 commented 3 months ago

Hi @jordikitto, thanks for bringing this to our attention -- Looks like there's some cases that finding the checkout of code-connect doesn't cover completely.

Would you be willing to share what the output of running xcodebuild -project .xcodeproj -showBuildSettings locally is for you, and how you're adding the package to your project? That would help to determine a proper solution.

Workaround wise, as you've identified you can add a symlink in order to trick the parser to finding the right location. Another option is to create a dummy Package.swift file somewhere that adds a dependency to code-connect and then configure the configuration file with the relevant include glob paths (And then run code connect from that directory).

jordikitto commented 3 months ago

Hi @jyyang0

Here is the output of xcodebuild -project .xcodeproj -showBuildSettings -> xcodebuild-output.txt

As for the project structure, it looks like such:

/<REPO_NAME>/
  <PROJECT_NAME>.xcodeproj <-- Code Connect package added here
  <PROJECT_NAME>.xcworkspace
  /<PROJECT_NAME>/
  /LOCAL_PACKAGE_1/
  /LOCAL_PACKAGE_2/
  /LOCAL_PACKAGE_3/
  /LOCAL_PACKAGE_DESIGN_SYSTEM/
    Package.swift <-- Code Connect package added here as well

The package is added to the project like so:

  1. Select Project
  2. Select "Package Dependencies"
  3. Click "+"
  4. Search for Code Connect via GitHub URL, add to project
  5. Click "Add package"
  6. Add Figma Library to app target, and don't add figma-swift executable to target (Left as none, this caused build errors if added)

The package is added to the LOCAL_PACKAGE_DESIGN_SYSTEM package as per the installation instructions In Package.swift:

  1. Add package dependency to dependencies
  2. Add product dependencies to package targets dependencies

This all builds successfully, with a FigmaConnection file in LOCAL_PACKAGE_DESIGN_SYSTEM package publishing successfully to Code Connect.

If it makes any difference, I open the "project" via the xcworkspace when developing. I'm also using Xcode 16 Beta 6.

jordikitto commented 2 months ago

Hey @jyyang0, any updates on this one? Would be really great if we could integrate this into our CI for our large project but this is making it quite difficult :(

jyyang0 commented 2 months ago

Sorry about the delay -- Looking at your setup it seems like our current system is making some assumptions about xcodeproj based workspaces. which is failing -- In the next release that's coming out (1.0.7) we've added the ability to specify a swiftPackagePath which you can use to refer to the Package.swift file. Another option here is to run figma connect ... in your LOCAL_PACKAGE_DESIGN_SYSTEM (so there's no ambiguity about whether theres an xcodeproj or SPM managed project) which should also unblock you.

This isn't a perfect solution and we're still iterating on how we can make the system more robust as a whole to support a variety of xcode project configurations, but this feedback is really helpful!

raphschwander commented 2 months ago

Hello @jyyang0 I am experiencing the same issue on CLI 1.1.4

  1. Add Figma through SPM in Xcode under "Package Dependencies"
  2. Run CLI command from the project root folder

It cannot locate the checkout of code-connect in derived data folder.

That's because it uses BUILD_DIR. xcodebuild -project .xcodeproj -showBuildSettings returns the project folder, BUILD_DIR is (/Users/raph/Developer/<project>/build) not the derived data folder.

BUILD_DIR only returns the correct derived folder path when building.

@jyyang0 what's the correct way to install it, I've followed instructions on https://github.com/figma/code-connect/blob/main/docs/swiftui.md ?

jordikitto commented 1 month ago

@raphschwander are you using local packages like myself? Or just single project?

raphschwander commented 1 month ago

@jordikitto just a single project.

tomduncalf-figma commented 1 month ago

Hey @raphschwander, sorry to hear you're continuing to have issues here. It feels like a good workaround for now might be for us to provide a way to override the checkout location manually, would that work?

raphschwander commented 1 month ago

hello @tomduncalf-figma , thanks for getting back to me. In the meantime, I used a hacky workaround as jyygang0 suggested to make it work. I only wanted to test the tool. Yes, that would be a workaround, but not very convenient, as finding the checkout location is not straightforward and AFAIK it can change (for instance after cleaning the derived data folder)

tylerjdoyle commented 2 days ago

+1 that I'm also experiencing this issue while not using a Package.swift file and instead adding the dependency directly in the project. The symbolic link workaround worked for me as well but this blocks our CI from doing the same thing.