switchupcb / copygen

Go generator to copy values from type to type and fields from struct to struct (copier without reflection). Generate any code based on types.
https://switchupcb.com/copygen-license-exception/
GNU General Public License v3.0
352 stars 22 forks source link

Error: the "type Copygen interface" could not be found in the setup file #35

Closed pobearm closed 1 year ago

pobearm commented 1 year ago

Please provide the following information.

Setup

YML

# Define where the code will be generated.
generated:
  setup: ./setup.go
  output: ./copygen.go

  # Define the optional custom templates used to generate the file (.go, .tmpl supported).
  # template: ./generate.go
# Define custom options (which are passed to generator options) for customization.
# custom:
#   option: The possibilities are endless.

Go

package copygen

import (
    "demo/admin/adapters/reposit/dal"
    "demo/admin/domain"
)

type Copygen interface {
    Basic(user *domain.AgrUser) *dal.SysUser
}

Output

Error

the "type Copygen interface" could not be found in the setup file

Generation

Environment

Operating System: windows 10 Copygen Version: 0.4.0 Golang Version : 1.19.4

switchupcb commented 1 year ago

Error occurs at parse.go#L154 indicating that the type Copygen Interface is not being detected in the setup.go file within the current directory (after it has been added to the keep). This means that the interface not in the go/types of the packages.Load function at parse.go#L143. There is not an explicit check on whether it is detected within the Keep using the ast loader.

Please show your file structure for this demo alongside the command used to run it @pobearm.

switchupcb commented 1 year ago

You can ignore this message unless you plan to fix this issue.

https://github.com/switchupcb/copygen/blob/19e910b3a024f6b4b1ef663859b28487b727f4dc/cli/config/yml.go#L28 gets the file path from the file location of the .yml file. https://github.com/switchupcb/copygen/blob/19e910b3a024f6b4b1ef663859b28487b727f4dc/cli/config/yml.go#L34 uses filepath.Dir which gets all parts but the last element of the path. It's expected that the user provides a .yml file as the loader path flag such that filepath.Dir is equivalent to the directory containing the loader. Such that filepath.Join(loaderDir, ./setup.go) resolves to loaderDir/setup.go.

The package does not fail to load.

assertCopygenInterface at https://github.com/switchupcb/copygen/blob/ea03ecfaa3468b66e34a9d41c908a91fc7f28044/cli/parser/ast.go checks for the interface via ast.

Hypothesis

All tested examples generate the output file in a separate directory from the setup file. Such that placing the setup file and output file in the same directory may result in undefined behavior. Otherwise, user is using outdated files (unsaved) or a wrong configuration that doesn't error (bug).

switchupcb commented 1 year ago

placing the setup file and output file in the same directory may result in undefined behavior.

This error occurs when the user uses multiple files in the same directory as the setup file, and the setup file isn't the first file. This happened because only the first file's AST in a directory was checked for the type Copygen interface.

Fix

parser.ParseFile parses a single file, which will always parse the correct (setup) file to receive the AST of the setup file. However, an error has been added to specify when the Copygen interface hasn't been found in the AST.

The packages loaded from packages.Load of the setup file generates a new AST. So this AST must be checked for a new Copygen interface because it matches to that loader's go/types. Unlike parseFile, there is no guarantee that a single file is present nor that the setup file is the first. So each file is checked until the Copygen interface is found.

switchupcb commented 1 year ago

Use

go install github.com/switchupcb/copygen@main

to update to a version with this fix.