# Buggy code
def resolve_extends(self, service_dict):
    if "extends" not in service_dict:
        return service_dict

    extends_options = self.validate_extends_options(
        service_dict["name"], service_dict["extends"]
    )

    if self.working_dir is None:
        raise Exception("No working_dir passed to ServiceLoader()")

    if "file" in extends_options:
        extends_from_filename = extends_options["file"]
        other_config_path = expand_path(self.working_dir, extends_from_filename)
    else:
        other_config_path = self.filename

    other_working_dir = os.path.dirname(other_config_path)
    other_already_seen = self.already_seen + [self.signature(service_dict["name"])]
    other_loader = ServiceLoader(
        working_dir=other_working_dir,
        filename=other_config_path,
        already_seen=other_already_seen,
    )

    other_config = load_yaml(other_config_path)
    other_service_dict = other_config[extends_options["service"]]
    other_loader.detect_cycle(extends_options["service"])
    other_service_dict = other_loader.make_service_dict(
        service_dict["name"],
        other_service_dict,
    )
    validate_extended_service_dict(
        other_service_dict,
        filename=other_config_path,
        service=extends_options["service"],
    )

    return merge_service_dicts(other_service_dict, service_dict)


# Traceback (KeyError)
$ docker-compose up -d
Traceback (most recent call last):
File "/Users/aanand/.virtualenvs/docker-compose/bin/docker-compose", line 9, in <module>
load_entry_point('docker-compose==1.4.0.dev0', 'console_scripts', 'docker-compose')()
File "/Users/aanand/work/docker/compose/compose/cli/main.py", line 39, in main
command.sys_dispatch()
File "/Users/aanand/work/docker/compose/compose/cli/docopt_command.py", line 21, in sys_dispatch
self.dispatch(sys.argv[1:], None)
File "/Users/aanand/work/docker/compose/compose/cli/command.py", line 27, in dispatch
super(Command, self).dispatch(*args, **kwargs)
File "/Users/aanand/work/docker/compose/compose/cli/docopt_command.py", line 24, in dispatch
self.perform_command(*self.parse(argv, global_options))
File "/Users/aanand/work/docker/compose/compose/cli/command.py", line 57, in perform_command
verbose=options.get('--verbose'))
File "/Users/aanand/work/docker/compose/compose/cli/command.py", line 78, in get_project
config.load(config_details),
File "/Users/aanand/work/docker/compose/compose/config/config.py", line 132, in load
service_dict = loader.make_service_dict(service_name, service_dict)
File "/Users/aanand/work/docker/compose/compose/config/config.py", line 156, in make_service_dict
service_dict = self.resolve_extends(service_dict)
File "/Users/aanand/work/docker/compose/compose/config/config.py", line 183, in resolve_extends
other_service_dict = other_config[extends_options['service']]
KeyError: 'foo'

# Fixed code
def resolve_extends(self, service_dict):
    if "extends" not in service_dict:
        return service_dict

    extends_options = self.validate_extends_options(
        service_dict["name"], service_dict["extends"]
    )

    if self.working_dir is None:
        raise Exception("No working_dir passed to ServiceLoader()")

    if "file" in extends_options:
        extends_from_filename = extends_options["file"]
        other_config_path = expand_path(self.working_dir, extends_from_filename)
    else:
        other_config_path = self.filename

    other_working_dir = os.path.dirname(other_config_path)
    other_already_seen = self.already_seen + [self.signature(service_dict["name"])]
    other_loader = ServiceLoader(
        working_dir=other_working_dir,
        filename=other_config_path,
        already_seen=other_already_seen,
    )

    base_service = extends_options["service"]
    other_config = load_yaml(other_config_path)

    if base_service not in other_config:
        msg = ("Cannot extend service '%s' in %s: Service not found") % (
            base_service,
            other_config_path,
        )
        raise ConfigurationError(msg)

    other_service_dict = other_config[base_service]
    other_loader.detect_cycle(extends_options["service"])
    other_service_dict = other_loader.make_service_dict(
        service_dict["name"],
        other_service_dict,
    )
    validate_extended_service_dict(
        other_service_dict,
        filename=other_config_path,
        service=extends_options["service"],
    )

    return merge_service_dicts(other_service_dict, service_dict)