Skip to content

compiled tpc resolves TypePHP dependencies relative to the current working directory #72

Description

@jefyokta

Environment

  • macOS
  • Homebrew

Description

i tried to make Homebrew formula to make global tpc bin, tpc currently assumes that paths such as vendor/... are relative to the user's current working directory.

Cause

The relevant code is in src/compiler.php:

typephp/src/compiler.php

Lines 14 to 18 in e90d624

if (!defined('TYPEPHP_ROOT_PATH')) {
define('TYPEPHP_ROOT_PATH', getcwd());
}
if (!defined('TYPEPHP_DEBUG')) {

Since the entry file does not include bin/bootstrap.php, where TYPEPHP_ROOT_PATH is defined, the code falls back to getcwd().

As a result, tpc looks for its own dependencies relative to the current working directory rather than the TypePHP installation directory.

I tested the behavior of getcwd() with a compiled TypePHP executable:

jefyokta@jefyokta child-dir % ./pwd_php
~/parent-dir/child-dir

jefyokta@jefyokta child-dir % cd ..

jefyokta@jefyokta parent-dir % ./child-dir/pwd_php
~/parent-dir

jefyokta@jefyokta parent-dir % cat ./child-dir/pwd.php
<?php

function main()
{
    echo getcwd() . PHP_EOL;
}

This shows that getcwd() returns the current working directory from which the executable is invoked, rather than the directory where the executable itself is located.

For example, when running:

cd /tmp/my-project
tpc project.yml

the compiler may attempt to resolve paths such as:

/tmp/my-project/vendor/...

instead of:

/path/to/typephp/vendor/...

This makes it difficult to use tpc as a globally installed executable because TypePHP's internal dependencies are expected to exist inside the user's current project.

My Temporary Solution

Anyway, I have already created a experimental Homebrew tap as a temporary workaround:

https://github.com/jefyokta/homebrew-typephp

The formula currently replaces the relevant constant/path handling:

https://github.com/jefyokta/homebrew-typephp/blob/0c033e0bf45c6880ecaa28eabd9af3653ffdcc51/Formula/tpc.rb#L41-L45

 -  define('TYPEPHP_ROOT_PATH', getcwd());
 + define('TYPEPHP_ROOT_PATH', getenv('TYPEPHP_HOME') ?: getcwd());

With this workaround, I set TYPEPHP_HOME to the correct TypePHP installation path. If TYPEPHP_HOME is not set, it falls back to getcwd() as before.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions